Skip to content

Commit e06ac26

Browse files
committed
docs(readme): how to deal with local state, fix the error from immer
1 parent 057f3da commit e06ac26

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ npm install react-model
8282
- [How can I disable the console debugger?](#how-can-i-disable-the-console-debugger)
8383
- [How can I add custom middleware](#how-can-i-add-custom-middleware)
8484
- [How can I make persist models](#how-can-i-make-persist-models)
85+
- [How can I deal with local state](#how-can-i-deal-with-local-state)
86+
- [actions throw error from immer.module.js](#actions-throw-error-from-immer.module.js)
8587

8688
## Core Concept
8789

@@ -750,3 +752,102 @@ Model({ Example }, JSON.parse(localStorage.getItem('__REACT_MODEL__')))
750752
```
751753
752754
[⇧ back to top](#table-of-contents)
755+
756+
### How can I deal with local state
757+
758+
What should I do to make every Counter hold there own model? 🤔
759+
760+
```tsx
761+
class App extends Component {
762+
render() {
763+
return (
764+
<div className="App">
765+
<Counter />
766+
<Counter />
767+
<Counter />
768+
</div>
769+
)
770+
}
771+
}
772+
```
773+
774+
<details>
775+
<summary>Counter model</summary>
776+
<p>
777+
778+
```ts
779+
interface State {
780+
count: number
781+
}
782+
783+
interface ActionParams {
784+
increment: number
785+
}
786+
787+
const model: NextModelType<State, ActionParams> = {
788+
state: {
789+
count: 0
790+
},
791+
actions: {
792+
increment: payload => {
793+
// immer.module.js:972 Uncaught (in promise) Error: An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft
794+
// Not allowed
795+
// return state => (state.count += payload)
796+
return state => {
797+
state.count += payload
798+
}
799+
}
800+
}
801+
}
802+
803+
```
804+
805+
</p>
806+
</details>
807+
808+
<details>
809+
<summary>Counter.tsx</summary>
810+
<p>
811+
812+
```tsx
813+
814+
const Counter = () => {
815+
const [{ useStore }] = useState(() => Model(model))
816+
const [state, actions] = useStore()
817+
return (
818+
<div>
819+
<div>{state.count}</div>
820+
<button onClick={() => actions.increment(3)}>Increment</button>
821+
</div>
822+
)
823+
}
824+
825+
export default Counter
826+
```
827+
828+
</p>
829+
</details>
830+
831+
[⇧ back to top](#table-of-contents)
832+
833+
### actions throw error from immer.module.js
834+
835+
```
836+
immer.module.js:972 Uncaught (in promise) Error: An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft
837+
```
838+
839+
How to fix:
840+
841+
```tsx
842+
actions: {
843+
increment: payload => {
844+
// Not allowed
845+
// return state => (state.count += payload)
846+
return state => {
847+
state.count += payload
848+
}
849+
}
850+
}
851+
```
852+
853+
[⇧ back to top](#table-of-contents)

0 commit comments

Comments
 (0)