effects are magically part of reducers
See original GitHub issueI’m planning on giving a HyperApp presentation to the other developers at work. When I explain effects, I’m going to have a sample running in codepen similar to this:
app({
model: {
city: "",
wx: { }
},
reducers: {
setCity: (model, e) => ({ city: e.currentTarget.value }),
setWeather : (model, formattedData ) => ({ wx: formattedData })
},
effects: {
getWeather : (model, reducers) => {
axios.get'http://api.openweathermap.org/data/2.5/weather'....
reducers.setWeather(obj);
})
}
},
view: (model, reducers) => (
<div>
<input type="text" oninput={ (e) => ( reducers.setCity(e)) } />
<input type="button" value="Get Weather" onclick={ _ => reducers.getWeather() } />
{ Object.keys(model.wx).map((key, index) => (<div>{key} : {model.wx[key]} </div>)) }
</div>
)
})
The part that they are going to be questioning is this:
<input type="text" oninput={ (e) => ( reducers.setCity(e)) } />
<input type="button" value="Get Weather" onclick={ _ => reducers.getWeather() } />
Since reducers are passed into the view and not the effects… how is it that we can call reducers .getWeather() and it magically uses the effect? How do I explain this eloquently - or can I change the code to make it not seem so magic?
Thanks, Chad
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Why does a Redux reducer have to be side-effect free?
Consider a reducer that does this: (state, action) => { state.prop += 1; return state; } . This is not side-effect free but...
Read more >Reducer Composition with Effects in JavaScript #1528 - GitHub
Problem: Side Effects and Composition We discussed effects in Redux for ages ... composing nested reducers will require some magic (right now, in...
Read more >Understanding the magic behind @ngrx/effects - InDepth.Dev
As you may know, an action is a constituent of a reducer, as well as of an effect. NgRx ensures that actions are...
Read more >Switch to redux-saga. Keep side-effects isolated from your reducers ...
Keep side-effects isolated from your reducers and action creators.” is published by Eric Elliott. ... Make some magic. #JavaScript.
Read more >Immutable Update Patterns - Redux
Structuring Reducers > Immutable Update Patterns: How to correctly update state immutably, with examples of common mistakes.
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
@cdeutmeyer Thanks for the question! This point is now well explained in the docs. I added cross-ref links and hinted that both reducers and effects conform what HyperApp calls actions.
thanks @dodekeract!! I’m kinda new at making posts!