DEV Community

Randy Rivera
Randy Rivera

Posted on • Edited on

Redux: Create a Redux Store

  • Hello Everyone, and welcome back to more lessons, and challenges provided by FreeCodeCamp! Today we'll be learning about Redux.

What is Redux?

  • Redux is a state management framework that can be used with a number of different web technologies, including React.
  • In Redux, there is a single state object that's responsible for the entire state of your application. This means if you had a React app with ten components, and each component had its own local state, the entire state of your app would be defined by a single state object housed in the Redux store.
  • This means that any time your apps wants to update state, it has to go through the Redux store. It honestly just makes it easier to track the state management in your app. app.
  • FreeCodeCamp wants us to create a Redux store with the provided code. We have a method called createStore() on the Redux object, which we use to create the Redux store. This method takes a reducer function as a required argument. We will definitely talk more about reducer function in later posts.
  • Let's declare a store variable and assign it to the createStore() method, passing in the reducer as an argument.
  • Code:
const reducer = (state = 10) => {
  return state;
}
Enter fullscreen mode Exit fullscreen mode
  • Answer:
 const store = Redux.createStore(reducer)
Enter fullscreen mode Exit fullscreen mode

Get State from the Redux Store

  • * You can also retrieve the current state held in the Redux object with the getState() method. The code from the above example is re-written more clear in the code editor. Use store.getState() to retrieve the state from the store, and assign this to a new variable currentState.
  • Code:
const store = Redux.createStore(
  (state = 10) => state
);

Enter fullscreen mode Exit fullscreen mode
  • Answer:
let currentState = store.getState()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)