Bootcamp is over, and I’ve started my first job as a junior front-end web developer!

Photo by Kari Shea on Unsplash
My first week at my new job, we discussed the possibility of using Redux in an upcoming React project, which led to me doing a lot of reading about its uses, implementation, advantages, and downsides. Here’s what I learned that might be helpful to you!
What is Redux?
Redux is a JavaScript library that allows you to store the entire state of your app in one place rather than being broken up between the many components of your app. With conventional state use, state is derived and stored within components and passed through props to other components that make use of it. This can sometimes result in the headache we know as ‘prop drilling’, where state must be passed through many many many components that don’t use it before reaching its final destination. With Redux, state can be distributed to the components that need it from one central source of truth!

The Main Players
The Store
There is only one store per app and it holds an object tree which represents the entire state of your app! It is generated using createStore() and comes with { subscribe, dispatch, and getState }. Let’s make an Ikea that has some couches in its inventory.
let store = createStore(Ikea)
The Actions
The action is the thing that happened within your component or as a result of some interaction that would cause a state change! Actions are described as objects, there can be lots of different types of actions, and can be dispatched to the reducer. In this case, couches can be bought and returned.
store.dispatch({ type: 'BUYCOUCH' })
store.dispatch({ type: 'RETURNCOUCH' })
The Reducers
Redux was invented because some guy (Dan Abramov) thought to build a state management library that was essentially a big reduce function. The state is never actually changed, but rather a new state is returned by the reduce function, meaning the state is immutable!
function ikea(state = 10, action) {
switch (action.type) {
case 'BUYCOUCH':
return state - 1
case 'RETURNCOUCH':
return state + 1
default:
return state
}
}
Want to display the updated couch inventory within your component? Use store.getState() to access the store within your component and voila!
<Inventory
value={store.getState()}
/>,
This is obviously a very simple example and seems like a lot of work when we could just use this.state and this.setState(). One of the downsides to Redux is how much setup and boilerplate there is upfront, but the nice thing about Redux is how well it scales to much larger apps. Consider it the next time you’re kicking off a rather large project.

Alternatives to Redux include the good ol’ fashioned way, Context, and the newest hottest thing, React Hooks! Each have their advantages and disadvantages, which will hopefully be covered in a future blog post. Our project ultimately went with React Hooks 😉


