Skip to content

0.4.0 Release

Compare
Choose a tag to compare
@minajevs minajevs released this 20 Mar 13:16
· 49 commits to master since this release

Calling other stores from actions

Sometimes you would like to call other store action from an action. You can't use React.useContext because of specific hook rules in React. Hook amount should never change during runtime and only way to supply that is to initialize all dependency contexts before bootstraping actions.

You can call actions in other stores by providing dependency contexts as a 3rd parameter to createStoreContext. Those contexts will be mapped to corresponding stores internally and will be available in stores object in {setState, action, stores} argument of action creator.

Example:

const [todoContext, Provider] = createStoreContext({todos: []}, {
    addTodo: ({state, setState}, todo) => {
        setState({todos: [...state.todos, todo]})
    },
})
const [mainContext, Provider] = createStoreContext({message: ''}, {
    someAction: ({setState, stores}, name) => {
        const { todos } = stores.todoContext // stores.todoContext is a "todo store" ({todos: [], addTodo: (todo) => void})
        const newMessage = `Hello, ${name}, you have ${todos.length} todos!`
        setState({message: newMessage})
    },
}, {todoContext})
...
// Usage
const todoStore = React.useContext(todoContext)
const mainStore = React.useContext(mainContext)
todoStore.addTodo('buy milk')
todoStore.addTodo('learn typescript')
mainStore.someAction('Dmitrijs')
// mainStore.message is "Hello, Dmitrijs, you have 2 todos!" 

Commits:

  • Added feature to call other stores afc27d1
  • Removed section about calling hooks inside actions fdce20c

v0.3.2...v0.4.0