Question: Always need to clone a state property in reducer?
See original GitHub issueHello again,
given follwing example:
// state has property 'list' which is an array().
const reducer = (state, action) => {
switch (action.type) {
case 'ADD_LIST_ITEM':
state.list.push('new item');
return Object.assign({}, state, { list: state.list });
}
};
Using this, a polymer element property bound by āstatePath: ālistāā doesnāt recognize the change of state.list.
When doing the following, everything works like desired:
const reducer = (state, action) => {
switch (action.type) {
case 'ADD_LIST_ITEM':
var tmp = state.list.splice(0);
tmp .push('new item');
return Object.assign({}, state, { list: tmp });
}
};
So does this mean, Iāve to copy/clone every property of the state in the reducer before changing it?
Regards, Michael
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
In Redux, is it necessary to do deep copy - Stack Overflow
Common Redux misconception: you need to deeply clone the state. ... Changed properties will be anyway new values, so no question of type...
Read more >Redux Fundamentals, Part 3: State, Actions, and Reducers
The official Redux Fundamentals tutorial: learn how reducers update state in response to actions.
Read more >Actions and reducers: updating state - Human Redux
So the resulting function takes the whole starting application state, the action to be processed, and returns the new application state.
Read more >React Hooks cheat sheet: Best practices with examples
To help demonstrate how to solve common React Hooks questions, ... The useReducer call returns the state property and a dispatch function.
Read more >Returning the State from the Reducer - Understanding Redux
You should not mutate the state received in your Reducer. Instead, you should always return a new copy of the state. Technically, you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
No worries.
If you have any questions, get me on slack rather than raise issues. Unless itās an issue of course š
https://polymer.slack.com
You donāt have to use this āpatternā. Google wrote their own unidirectional, UniFlow with achieves the same goal just with a different state manager.
Whatever you use, unidirectional is the way forward. Always push your values down and use Mediators correctly. By correctly, I mean using
readOnly
on the properties that push state upwards.