robinweser / alveron

Tiny (0.8kb) Elm-inspired state management for React
https://alveron.js.org
MIT License
73 stars 2 forks source link

Force effects to call actions #7

Closed robinweser closed 6 years ago

robinweser commented 6 years ago

Instead of exposing setState freely, we could even restrict effects more to just calling actions. Could be great in terms of testing and separation:

const model = []
const actions = {
  addUser: (state, user) => [
    ...state,
    user
  ]
}

const effects = {
  postUser: (actions, user) => fetch("api.com/users", { 
      method: 'POST', 
      body: JSON.stringify(user)
    }).then(res => res.json()).then(actions.addUser)
}