This is an example code from the easy peasy docs but the same question goes for createThunk from redux-toolkit:
function MyComponent() {
const saveTodo = useStoreActions(actions => actions.todos.saveTodo);
const onSaveClick = useCallback(
// We chain on the promise returned by dispatching the thunk
//
() => saveTodo('Learn easy peasy').then(() => {
// redirect on success
history.push('/done');
}),
[saveTodo]
);
}
- Lets say saveTodo saves a todo in store(changing the store). changing the store triggers a re-render of subscribed components. So, how does the "then" logic execute anyway?
- Lets say that saving the todo will not re-render the component immediately but after executing the logic in "then". What if I want to display a loading spinner while saving the todo and just then execute the "history.push..."?