react-waterfall-example icon indicating copy to clipboard operation
react-waterfall-example copied to clipboard

Demonstrate new syntax for actions

Open jtremback opened this issue 7 years ago • 0 comments

Hi, this demonstrates a new syntax for actions which looks like this:

const store = {
  actions: {
    // Set state multiple times in an action, e.g. for loading indicator
    doSomething: async ({ state, setState }, user) => {
      // Set state using setState
      setState({ loading: true })
      const response = await fetch('https://' + state.remote + '/users/' + user)
      const body = await response.json()
      // Set state by returning new state
      return {
        loading: false,
        user: { avatar: body.avatar_url }
      }
    }
  }
}

jtremback avatar Mar 05 '18 00:03 jtremback