Topics:
reduxpackagereact-reduxpackage- Redux state vs react component state
<Provider>andcreateStore(reducer)- One state object for entire application
reducerfunctions control the values for the state properties.containercomponents are React components that connect to the Redux stateactions- objects produced byaction creatorsthat will be fed through all reducers.mapStateToProps()&connect
- Run
create-react-app todoto create your starter application. - Now that you have created your
tododirectory, cd into it. yarn add redux react-reduxornpm install --save redux react-reduxThis command will install the needed dependencies.- You will create a todo list using React and Redux.
- Use the movies project as a reference.
- The general flow of steps will be to create your store, create your reducers, create your containers, and then create the action creators.
- When you add a new item to the todo array an action containing the new todo object will be dispatched through all of the reducers.
- To display the todo list you will create a container that receives the
todosarray as a prop and then usesmapto display it as an unordered list.
- Your application should have an input field, a submit button, and a list of items that represents your todo list.
- Your application's state tree should have a single property called
todos. It should take the same form as the object shown below.
{
todos: []
}
- Each
todoitem that is in thetodosarray should have the following format:
{
value: 'Walk the dog.',
completed: false
}
- You will create your store in
src/index.js. The<Provider >component will wrap<App />and you will pass the created store into<Provider >as one of its properties. Use this repository as a reference.
- When you type a new todo list item into the input field and press the submit button you should call an action creator that adds a new todo item to the
todosarray on the application state tree. - When the user presses submit you will invoke the appropriate action creator which will then have its new action fed through all of the reducers.
- You will display the todo list by creating a container that receives the application's
todosarray as a prop. That container then usesmapto display the array. - When you click on each todo list item you will dispatch an action that will toggle that todo item's
completedproperty to eithertrueorfalse. You will need to send theidproperty along with whatcompletedshould be set to. Thetodosreducer will return a brand new array that will replace the old array. We do not mutate the original array but rather replace it with a brand new version.
- You should only need one reducer. This reducer will control the
todosarray property on the state tree. - You will have several action creators. One for adding a new todo item and another for toggling each todo item.
- Containers require
connectand amapStateToProps(state)function to read from the state tree. - Actions creators should be passed inside an object as the second argument to the
connectfunction inside components that need access to the Redux store. - http://redux.js.org/ has a todo list as an example project in their documentation. Feel free to use this as a reference as well.
- Implement the ability to delete todo list items. You can create a button next to each todo list item and when it is pressed it will call an action creator that will dispatch an action that removes the specified todo list item from the
todosarray. - Use
localStorageto make the data persist.