2.9 KiB
2.9 KiB
id | title | challengeType | isRequired |
---|---|---|---|
5a24c314108439a4d403614b | Create a Redux Store | 6 | false |
Description
store
. This is the first important principle to understand when learning Redux: the Redux store is the single source of truth when it comes to application state.
This also means that any time any piece of your app wants to update state, it must do so through the Redux store. The unidirectional data flow makes it easier to track state management in your app.
Instructions
store
is an object which holds and manages application state
. There is a method called createStore()
on the Redux object, which you use to create the Redux store
. This method takes a reducer
function as a required argument. The reducer
function is covered in a later challenge, and is already defined for you in the code editor. It simply takes state
as an argument and returns state
.
Declare a store
variable and assign it to the createStore()
method, passing in the reducer
as an argument.
Note: The code in the editor uses ES6 default argument syntax to initialize this state to hold a value of 5
. If you're not familiar with default arguments, you can refer to the ES6 section in the Curriculum which covers this topic.
Tests
tests:
- text: The redux store exists.
testString: assert(typeof store.getState === 'function', 'The redux store exists.');
- text: The redux store has a value of 5 for the state.
testString: assert(store.getState()=== 5, 'The redux store has a value of 5 for the state.');
Challenge Seed
const reducer = (state = 5) => {
return state;
}
// Redux methods are available from a Redux object
// For example: Redux.createStore()
// Define the store here:
Solution
const reducer = (state = 5) => {
return state;
}
// Redux methods are available from a Redux object
// For example: Redux.createStore()
// Define the store here:
const store = Redux.createStore(reducer);