question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

effects are magically part of reducers

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jorgebucarancommented, Feb 25, 2017

@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.

1reaction
cdeutmeyercommented, Feb 25, 2017

thanks @dodekeract!! I’m kinda new at making posts!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found