Last updated
Last updated
If you haven't worked with Redux, it's highly recommended (possibly indispensable!) to read through the (amazing) and/or watch this .
As minimal as Redux is, the challenge it addresses - app state management - is a complex topic that is too involved to properly discuss here.
Let's start creating a slice to manage our Homescreen data and call it HomescreenSlice
.
An empty folder .../Homescreen/slice/
Redux manages your state so we have to declare our state first. We can create a types.ts
file in our slice. Types are crucial for efficient and safe development. Your compiler and code completion will understand the shape of your state and help you code the rest of your project faster and safer.
.../Homescreen/slice/types.ts
types/RootState.ts
Our slice's initial state
Actions we can trigger
Reducers that decide how the state will change, given the action received
.../Homescreen/slice/index.ts
Let's add our slice to the redux state. We can write a simple 'hook' and use it in our component(whichever you want)
.../Homescreen/slice/index.ts
Let's use the hook we created above in our component
.../Homescreen/index.tsx
🎉 Good News: You don't need to write this boilerplate code by hand, the slice
generator will generate it for you. ✓
yarn generate slice
Now that you are adding another slice
to your state you also need to declare this in your types/RootState.ts
file. Since we are adding Redux slices asynchronously with , the compiler cannot tell what the Redux State is during the build time. So, we explicitly declare them types/RootState.ts
file:
Fortunately, handles most of the work for us. To create our slice, we create a index.ts
file in our folder as well. This will be responsible for: