How does actions change the state?
See original GitHub issueI’m wondering How does actions change the state? Is it similar to redux? I’m looking at the counter example which is pretty simple but is it immutable?
actions: {
down: state => ({ count: state.count - 1 }),
up: state => ({ count: state.count + 1 })
}
##Lets say an example like this
var state = {
property: {
propertyChild: [{
city: 'New York'
},
{
city: 'Santiago'
},
{
city: 'Tokyo'
}
]
}
}
what if I wanted to change the second object in propertyChild?
what would be the proper way to update that?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Actions and reducers: updating state - Human Redux
In Redux all changes to the application state happen via an "action." An "action" in Redux terms is simply "a thing that happened"...
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 >Why is it beneficial to change state w/ actions in redux?
State is read-only. The only way to change the state is to emit an action, an object describing what happened. This ensures that...
Read more >Redux: Describing State Changes with Actions - Egghead.io
The only way to change the state tree is by dispatching an action. An action is a plain JavaScript object, describing in the...
Read more >Changing the System State Using a Custom Action - Win32 apps
Custom actions that set properties, feature states, component states, or target directories, or schedule system operations by inserting rows ...
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
@codingphasedotcom
We wrap each action you pass to the app call in a function like this:
This way we can update the state every time an action returns. This in turn causes the UI to re-render. Note that in detail, this is more efficient than just re-rendering every single time. We only re-render the UI on the next browser repaint.
Yes, quite so.
Yes, it is. The way we update the current state is by replacing it with a new state, never mutating the previous one.
@codingphasedotcom I would recommend sticking with plain old JavaScript until you have to get into really nested state, at which point qim is a pretty nice library if you aren’t happy with immutability-helper. The default for return of an action is a shallow merge like @selfup said. It would be like redux only having 1 reducer that just does,
state = Object.assign({}, state, actionResult)
. You could also change that with a custom resolve if you want different behavior.