Clone the towers app from here, run npm install
, then npm start
.
The towers app displays a tower with two buttons: Raise and Lower, which raise and lower the tower respectively.
Its purpose is as a teaching aid in learning Redux. :-)
- This app doesn't (yet) use
react-redux
. How is the view updated?
We will now add Redux to this app.
Install redux and react-redux:
npm install redux react-redux --save
Create the store. To do this, create a new file, store.js
.
- Import
createStore
from redux. - Import
towersReducer
from your reducers file (which you will create in the next step). - Initialize the store by calling
createStore
and passing it your reducer.
Create the reducer. To do this, create the reducers file (which you imported in the previous step).
-
Export the reducer function. It should take as arguments the
state
object (which it should default to a default state) and anaction
. -
Your reducer file will check the state and action and return a new state, so it needs to know the action types! Import two constants from an actions file (which again you will create in a moment). For example:
import { RAISE_ACTION, LOWER_ACTION } from "./actions/towers";
-
Create a
switch
statement on theaction.type
(one of the action constants you imported in the previous substep), for example:switch(action.type) { case ADD_ACTION: return { ...
-
In the body of each
case
, return an object representing the new state, based on the action type and the current state. Remember to use immutable programming! -
The
default
case should just return the current state.
-
Action constants are just constants for the actions. You imported these in the previous step. The value is a string, eg 'RAISE'.
-
Write the action creators. These functions (eg
createRaiseAction()
) should return an action object, for example:{ type: RAISE_ACTION, }
We will now replace local component state with Redux.
First step is to edit index.js
and wrap our component tree in a <Provider>
:
-
In
index.js
, import the (named import)Provider
from redux. -
Import your store from the file you created it in.
-
Wrap the entire app in the Provider, for example:
ReactDOM.render( <Provider store={towersStore}> <React.StrictMode> <App /> </React.StrictMode>{" "} </Provider>, document.getElementById("root") );
Now to wire the App to Redux. We will again use react-redux
with its hooks.
-
Import the (named)
useDispatch
anduseSelector
from react-redux. -
Import your action creator functions.
-
Remove any local state corresponding to the height of the tower.
-
In the
App
component, create adispatch
variable fromuseDispatch()
. -
Again in the
App
component, create aheight
variable from a call touseSelector()
. This takes as an argument a function that retrieve the part of the state object that you want. For example:useSelector((state) => state.height);
Assign this to your height variable.
-
In the
raise()
andlower()
functions, remove any code that uses local state. Replace it with a call todispatch()
. This should take as argument a call to the appropriate action creator you imported earlier.
Run the app and see if it works!