
Then, by calling useDispatch as dispatch and passing setColor in our onPress callback, we can send an action to the appropriate reducer case. Grabbing our current state with, we set it as the data entry in our Flatlist element. We import useDispatch and useSelector from React-Redux. The useDispatch hook allows us to dispatch actions. For this, we'll use the useSelector hook, which will give us access to our redux state. Now, all we need is to be able to read the current state in homeScreen.js and dispatch an action to our reducer. We have successfully set up a Redux system for our app.Afterwards, we can import and add the slice to the root reducer of the store. Then an action creator is automatically generated using the name of the reducer as the action type. Here, we set the case functions defined in our reducer to colorSlice.actions. This is because Redux Toolkit allows us to create actions on the fly. Notice that we didn't define any action objects in our code.
REACT REDUX TOOLKIT CODE
But, with createSlice available through Redux Toolkit, we can write mutating code in reducers and have it converted into immutable copies. When writing core Redux logic, it is essential to avoid directly mutating the state value. Note: We can only write mutating code inside the createSlice or createReducer API. In the code block, we take the result of the randomRgb function and add it to the original array of colors.

The configureStore also automatically sets up the Redux DevTools Extension and some middleware. It uses the configureStore API in place of the createStore API from core Redux to build a store.

Actions require reducers to be successfully carried out. It is required that actions specify a payload and a type attribute that describes what kind of change is to be made to the state. Redux glossary ActionĪn action is a simple object that indicates a desire to modify a state in the Redux store. The way these are implemented changes when using Redux Toolkit in place of core Redux. To write immutable state updating code.It was designed to simplify the writing of common Redux logic and resolve the usual difficulties of using the core Redux library, such as: Redux Toolkit is Redux's official toolset for developing efficient React-Redux apps.
