Redux Paradox
Yani A.
Posted on October 31, 2021
Coming to the end of my part-time software engineering course with Flatiron School I was met with a challenge of incorporating Redux in my final project using React and JavaScript on the frontend and Ruby on Rails for the backend.
Redux is a state management tool that is compatible with JavaScript applications. It is supposed to make it easier for when you need the same state to be shared between multiple components and reflected in different parts of the application. It also comes in handy when the application states change frequently and different components can initiate changes simultaneously.
Seeing that my application is far from having a medium-to-large codebase with several developers working on it, it was an added step that I needed to make, that kind of defeated the whole purpose of Redux being a tool that improves the developer's experience. It was good to know how it works with React though.
In my final project, I am aiming to build an ecommerce application that is promoting Malaysian made handicrafts and products to help support micro businesses in rural areas in the country. I want it to help build the overall nation's economy and help increase job opportunities for locals. I had used Redux specifically for five cases:
- FETCH_CATEGORIES
- ADD_CATEGORY
- ADD_ITEM
- DELETE_ITEM
- EDIT_CATEGORY
First install Redux:
Redux + Plain JS template
npx create-react-app my-app --template redux
Redux + TypeScript template
npx create-react-app my-app --template redux-typescript
OR install it as a package on NPM or in a Node application:
NPM
npm install redux react-redux
Yarn
yarn add redux
In your index.js
file is where all the "action" happens. This is where you import {createStore} from 'redux'. Then, set up your Redux store by declaring store assigned to createStore(). To enable React to associate with the Redux store is by using the React-Redux package by importing {Provider}. It will wrap the <App/>
and pass in the store as a property.
Next create a reducers and actions folder, where you will create say a rootReducer file and an action creator file. In the rootReducer you would need to declare it passing in a state and an action
as arguments, returning the state. Then you would need to define an initial state. Lastly, at the bottom of the file, write export default rootReducer. Not forgetting to import the rootReducer in the index.js. You can then pass in the rootReducer in the createStore function. Following this, you're able to {connect} to components.
Other things I grew to like while building this project was client side routing and presentational components. The longer I learn how to code the more I’m veering towards front-end development. Perhaps I owe it to all those years in the media as an editorial assistant and writer, where I was fixated on the perfect magazine cover, photo shoots spread and layouts for our articles. Over a decade and some change, I’ve gone full circle and back to obsessing over presentation. Queue, the “presentation component”. Being mostly stateless, they’re simple and beautiful in doing what they do best - receiving props from their parent components and rendering! There’s just that satisfaction of having it all “work” for a change. With that said, let's hope we're able to leverage on using React and Redux in our near futures.
Resources and citation on REDUX:
Posted on October 31, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.