You should be able to:
- Create a Redux store (with a proper reducer and any necessary middleware)
- Write action types and creators corresponding to your store
- Subscribe to store changes
- Dispatch state-changing actions to the store
Redux is used to be a centralized place for our state to live in. Its main use case is when the state of our application gets too large. When this happens, it becomes difficult to maintain in React alone. If we declare our state "too high" and that data is needed in a distant child, the only way React can manage this is to send state
as props
through many layers of components. This is called "prop drilling." If we declare state "too low," and a distant ancestory needs that data, React is unidirectional and we are not supposed to "pass state back up."
- The store is a single source of truth
- Only one store can be invoked per app βοΈ
- State is read-only
- Changes are made through actions which flow through your pure reducer
Which of these are methods that are accessible directly from the Redux store? Select all that apply.
Option | Explanation | Documentation | |
---|---|---|---|
createStore |
This is from Redux itself, not the store | π | |
reducer |
We create this ourselves and pass it into the store | π | |
βοΈ | getState |
One of the functions that is given to us by the store | π |
applyMiddleware |
From Redux itself, not the store | π | |
βοΈ | dispatch |
One of the functions that is given to us by the store | π |
βοΈ | subscribe |
One of the functions that is given to us by the store | π |
unsubscribe |
You can say that this is technically given to us by the Redux store, but it is the return value of the subscribe function |
π |
Use Redux? | Option | Explanation |
---|---|---|
βοΈ | A browser-based game with lots of async user events | Keeping track of lots of users events that might affect state means it's a pain to keep that all coordinated while making sure each user gets the updates. The single source of truth aspect of Redux helps with this. |
βοΈ | An ecommerce store that tracks user authentication across views | Tracking auth state across views is a good use case for Redux. The single source of truth aspects prevents us from having to pass auth state to several different components. |
βοΈ | A dashboard that updates a series of graphs in real time | Same as above. Keeping multiple graphs (components) updated is tedious if we have to pass state around everywhere. |
π« | A fan wiki where users can read hundreds of articles | Is there really state being passed around everywhere and shared among multiple views and components? Do we need to keep things in sync? Are there complicated state changing actions? All we need to do here is get the articles and put them in state and display them. There can be a lot contained in state (articles) but state is not complicated. |
Option | Explanation | |
---|---|---|
Redux is part of the React library | Separate library. | |
React is part of the Redux library | Separate library. | |
Redux is a library for managing components | That's React's job. | |
βοΈ | Redux is a library for managing state | |
βοΈ | Redux can be used with any JavaScript app | It's a separate library so it can be used with anything. |
Redux can only be used with React | It's definitely used with React a lot, and a lot of blog posts make it seem that way, but, again, it's a separate library that can be used with any JavaScript app if you need a centralized place to manage state. |
Extra resources: