hyperapp icon indicating copy to clipboard operation
hyperapp copied to clipboard

effects are magically part of reducers

Open cdeutmeyer opened this issue 9 years ago • 6 comments

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

cdeutmeyer avatar Feb 25 '17 19:02 cdeutmeyer

@cdeutmeyer please use

```javascript

// source code goes here

```

for proper formatting, it's nearly impossible to read.

FlorianWendelborn avatar Feb 25 '17 19:02 FlorianWendelborn

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

cdeutmeyer avatar Feb 25 '17 19:02 cdeutmeyer

@cdeutmeyer Regarding your question, that's because the signature of view is actually (model, actions), not (model, reducers). actions are effects + reducers.

So everything is behaving as intended.

FlorianWendelborn avatar Feb 25 '17 19:02 FlorianWendelborn

I don't see any magic. Both are actions.

Btw, that's why i kinda lika destruction feature. So in my micro app (again elm architecture) i just pass one single context argument to the views, reducers, effects and subscriptions - { state, actions, props, params, children, data }. Destruction kinda enforce good names and "best practices" all the time.

tunnckoCore avatar Feb 25 '17 19:02 tunnckoCore

OK, thanks for the formatting help and explanation.. :)

cdeutmeyer avatar Feb 25 '17 19:02 cdeutmeyer

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

  • https://github.com/hyperapp/hyperapp/wiki/api#reducers
  • https://github.com/hyperapp/hyperapp/wiki/api#effects

jorgebucaran avatar Feb 25 '17 19:02 jorgebucaran