Skip to content

Commit b4c859d

Browse files
committed
more intelligent setter
1 parent f59593b commit b4c859d

File tree

5 files changed

+81
-16
lines changed

5 files changed

+81
-16
lines changed

example/src/App.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,24 @@ const Arr = () => {
135135

136136
return (
137137
<div>
138-
<h6>arrays: {array.map((a) => a + ', ')}</h6>
138+
<h6>arrays: {array?.map((a) => a + ', ')}</h6>
139139
<Button
140-
onPress={() => setState((array) => [...array, (array.pop() || 0) + 1])}
140+
onPress={() =>
141+
setState((array) =>
142+
array ? [...array, (array.pop() || 0) + 1] : [1, 2, 3]
143+
)
144+
}
141145
title='Increase Array'
142146
/>
143147
<Button
144-
onPress={() => setState((array) => array.slice(0, array.length - 1))}
148+
onPress={() =>
149+
setState((array) =>
150+
array ? array.slice(0, array.length - 1) : [1, 2, 3]
151+
)
152+
}
145153
title='Decrease Array'
146154
/>
155+
<Button onPress={() => setState()} title='Reset Array' />
147156
</div>
148157
)
149158
}

example/src/redux/store.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ const store = configureStore({
1717
})
1818
setConfig({
1919
cleanup: true,
20-
setter: (state, payload) => {
21-
console.log('using custom setter for type '+(typeof state));
22-
switch (typeof state) {
23-
case 'object':
24-
return { ...state, ...payload }
25-
case 'array':
26-
return [...state, ...payload]
20+
setter: (existingState, payload) => {
21+
console.log('using custom setter for type ' + typeof state)
22+
switch (existingState?.constructor) {
23+
case Object:
24+
return payload?.constructor === Object
25+
? { ...existingState, ...payload }
26+
: payload
27+
case Array:
28+
return payload?.constructor === Array
29+
? [...existingState, ...payload]
30+
: payload
2731
default:
2832
return payload
2933
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-redux-state-hook",
3-
"version": "1.0.0-alpha.10",
3+
"version": "1.0.0-beta.1",
44
"description": "Create runtime redux state",
55
"keywords": [
66
"redux-state",

src/reducers.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,16 @@ export const setWith = (object, path, value, index = 0) => {
102102
return setWith(object[paths[index]], paths, value, ++index)
103103
}
104104

105-
const _setter = (stateValue, payload) => {
106-
switch (stateValue?.constructor) {
105+
const _setter = (existingState, payload) => {
106+
switch (existingState?.constructor) {
107107
case Object:
108-
return { ...stateValue, ...payload }
108+
return payload?.constructor === Object
109+
? { ...existingState, ...payload }
110+
: payload
109111
case Array:
110-
return [...stateValue, ...payload]
112+
return payload?.constructor === Array
113+
? [...existingState, ...payload]
114+
: payload
111115
default:
112116
return payload
113117
}
Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,52 @@
11
---
22
id: using-redux-state
3-
title: state
3+
title: using redux state
44
---
5+
6+
```js
7+
import React from 'react'
8+
import useReduxState from 'use-redux-state-hook'
9+
10+
const App = () => {
11+
useReduxState({
12+
name: 'todos',
13+
state: [1,2,3],
14+
})
15+
// creates state in the store with key = todos and value = [1,2,3]
16+
}
17+
```
18+
## controlling how initial state should be set
19+
When you use the `redux-state-hook` with a default state, under the hood it sets the initial state in the store with the given name.<br/>
20+
`redux-state-hook` has an intelligent setter function which determines how your state will be set based on the payload **(referring the initial state as the payload)**.
21+
When array is passed as the payload setter assumes you will append the payload with the current state array.<br />
22+
Given the example below the initial todos state has `[1,2]` value, setter will push to the existing array state if the payload is array otherwise will replace the state.
23+
```js
24+
import React from 'react'
25+
import useReduxState from 'use-redux-state-hook'
26+
27+
const App = () => {
28+
// existing todos state = [1,2]
29+
useReduxState({
30+
name: 'todos',
31+
state: [3],
32+
})
33+
// later todos state = [1,2,3]
34+
}
35+
```
36+
Same logic applies to json object but [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive) payload value replaces the existing state.<br/>
37+
You can handle the setter behaviour by passing a reducer function.
38+
```js
39+
import React from 'react'
40+
import useReduxState from 'use-redux-state-hook'
41+
42+
const App = () => {
43+
// existing todos state = [1,2]
44+
useReduxState({
45+
name: 'todos',
46+
state: [3],
47+
reducer: (existingState, payload) => existingState ? [...payload, ...existingState] : payload
48+
// custom setter function prepends to the existing state
49+
})
50+
// later todos state = [3,1,2]
51+
}
52+
```

0 commit comments

Comments
 (0)