Skip to content

Commit 079efdc

Browse files
authored
documentation
1 parent 6662c21 commit 079efdc

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

README.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,139 @@
55
[![DeepScan grade](https://deepscan.io/api/teams/10012/projects/17109/branches/380047/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=10012&pid=17109&bid=380047)
66
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/ashish-r/create-global-state-selector/blob/master/LICENSE)
77
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fashish-r%2Fcreate-global-state-selector.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fashish-r%2Fcreate-global-state-selector?ref=badge_shield)
8+
9+
# Welcome to StackEdit!
10+
Create global state selectors from local redux slice selectors.
11+
In `redux` each of the slices are autonomus and final store structure is defined by how the individual slices are merged with `combineReducers` . `createGlobalStateSelector` takes local slice selectors and the slice structure to return global state selectors.
12+
13+
## Install
14+
15+
npm i create-global-state-selector
16+
17+
## Uses
18+
The example below uses `redux-toolkit` however you can use this with any standard Flux pattern that has multiple independent stores / slices, and are merged together with `combineReducers`.
19+
20+
// personalDetailsSlice.js
21+
22+
import { createSlice } from '@reduxjs/toolkit';
23+
24+
const sliceKey = 'personalDetails';
25+
const initialState = {
26+
name: 'Ashish',
27+
age: '26',
28+
isEligibleToDrink: true,
29+
};
30+
31+
const { actions, reducer } = createSlice({
32+
name: sliceKey,
33+
initialState,
34+
reducers: {
35+
setName(state, {payload}) {
36+
state.name = payload;
37+
},
38+
setAge(state, {payload}) {
39+
state.age = payload;
40+
},
41+
setIsEligibleToDrink(state) {
42+
state.isEligibleToDrink = selectLocalAge(state) >= 18;
43+
}
44+
},
45+
});
46+
47+
export default reducer;
48+
export const { increment, decrement } = actions;
49+
export sliceKey;
50+
export function selectLocalName(state) {
51+
return state.name;
52+
}
53+
export function selectLocalAge(state) {
54+
return state.age;
55+
}
56+
export function selectLocalIsEligibleToDrink(state) {
57+
return state.isEligibleToDrink;
58+
}
59+
60+
---
61+
// store.js
62+
63+
import { createStore, combineReducers } from 'redux';
64+
import personalDetailsReducer, { sliceKey as personalDetailsSliceKey } from personalDetailsSlice;
65+
66+
const reducer = combineReducers({
67+
[personalDetailsSliceKey]: personalDetailsReducer,
68+
});
69+
const store = createStore(reducer);
70+
// { personalDetails : { name: 'Ashish', age: '26', isEligibleToDrink: true } }
71+
72+
export default store;
73+
74+
---
75+
// selectors.js
76+
import createGlobalStateSelector from 'create-global-state-selector';
77+
import {
78+
sliceKey as personalDetailsSliceKey,
79+
selectLocalName,
80+
selectLocalAge,
81+
selectLocalIsEligibleToDrink
82+
} from personalDetailsSlice;
83+
84+
export const {
85+
selectName,
86+
selectAge,
87+
selectIsEligibleToDrink
88+
} = createGlobalStateSelector(
89+
{
90+
selectName: selectLocalName,
91+
selectAge: selectLocalAge,
92+
selectIsEligibleToDrink: selectLocalIsEligibleToDrink,
93+
},
94+
personalDetailsSliceKey
95+
);
96+
97+
// Global selectors created from local slice selectors
98+
// selectName({ personalDetails : { name: 'Ashish' } }) // 'Ashish'
99+
// selectAge({ personalDetails : { age: 26 } }) // 26
100+
101+
102+
103+
104+
105+
106+
## API Examples (createGlobalStateSelector)
107+
`createGlobalStateSelector` can accept both object of local slice selector functions or single local slice selector function .
108+
109+
### Pass an object of local slice selectors
110+
111+
const { selectX, selectY, selectZ } = createGlobalStateSelector(
112+
{
113+
selectX: (state: Record<string, any>): number => state.x,
114+
selectY: (state: Record<string, any>): number => state.y,
115+
selectZ: (state: Record<string, any>): string => state.z,
116+
},
117+
'a',
118+
'b'
119+
);
120+
121+
// Final store signature after combineReducers
122+
const store = { a: { b: { x: 55, y: 65, z: 'temp' } } };
123+
124+
selectX(store) // 55
125+
selectY(store) // 65
126+
selectZ(store) // 'temp'
127+
128+
### Pass a local slice selector
129+
130+
const selectZ = createGlobalStateSelector(
131+
(state: Record<string, any>): number => state.z,
132+
'a',
133+
'b'
134+
);
135+
136+
// Final store signature after combineReducers
137+
const store = { a: { b: { x: 55, y: 65, z: 'temp' } } };
138+
139+
selectZ(store) // 'temp'
140+
141+
## License
142+
143+
MIT

0 commit comments

Comments
 (0)