Redux Paradox 
Tags: redux, react, javascript, beginners
Published: 2021-10-31 03:45:02

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](https://github.com/yani82/malaysian-creation-fe) 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.

![](https://3.bp.blogspot.com/-5sQdvwcF0rM/WX1oHSL2bbI/AAAAAAAAnPU/HbW8W9iRSJ06nVBDDss8wiAPjWLdGg0MQCLcBGAs/s640/Actions%2B%25283%2529.png) 

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:

1. FETCH_CATEGORIES
2. ADD_CATEGORY
3. ADD_ITEM
4. DELETE_ITEM
5. 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:** 
* item 1 [Setting Up The Middleware](https://redux-observable.js.org/docs/basics/SettingUpTheMiddleware.html
) 
* item 2 [Complete React Tutorial (& Redux) #39 - Setting up Redux in React by The Net Ninja](https://www.youtube.com/watch?v=f87wPQMgF4c&t=41s
) 
* item 3 [Learning the basics - ReactJS and Redux]
(http://thewebstop.blogspot.com/2017/07/learning-basics-reactjs-and-redux.html)
* item 4 [Using Redux in React apps: When and why you need Redux](https://proxify.io/articles/using-redux-with-react)
* item 5 [Why Use React Redux?](https://react-redux.js.org/introduction/why-use-react-redux)