Mutating data in the Provider
Not only can you retrieve data using this Flux pattern, but you can also use it to update data. The pattern remains the same: you dispatch an action that would trigger the request to the server and, based on the outcome, the reducer will mutate the data with this result. Depending on whether or not it was successful, you could display a success message or an error message.
The code already has a form for adding a new item to a list—something that is not working yet. Let's create the mechanism to add items by updating the Provider for items
:
- The first step is to add new actions to the reducer in
ItemsContext
, which will be dispatched once we try to add a new item:// ... const reducer = (state, action) => { switch (action.type) { // ... + case 'ADD_ITEM_SUCCESS': + ...