Your project structure and basic dependencies are now ready. Great! Now, it is time to initialize the Redux actions, reducers and store. Additionally, we'll also define empty React components. Remember to separate presentation and container components.
This time, we'll add two container components (those which connect with the Redux store): BooksList
and BooksForm
and two presentational components: App
and Book
.
- Prepare the directory structure
- Create a
components
directory in thesrc
folder for all the presentational components. - Create a
containers
directory in thesrc
folder for all the container components. - Create a
actions
directory with theindex.js
file where you'll define all the Redux actions. - Create a
reducers
directory with two files:books.js
andindex.js
. The former will store the functions responsible for actions on Books, and the latter should combine all the reducers into one. For now, it will only combine thebooks.js
reducer. - Documentation: Combine Reducers
- Create a
- Implement the App component
- It should only wrap
BooksList
andBooksForm
in adiv
selector and display it. - Attach this component to render the function in the main
index.js
file.
- It should only wrap
- Implement the BooksList component
- It should display the books as an html table.
- Each row should contain Book ID, title and category.
- It should connect to the Redux store and fetch the books from the state.
- Implement the Book component
- It should accept a book object as a prop.
- It should represent a single row in the Books table.
- Implement the BooksForm component
- It should render a very simple form for adding books. The form should contain a book title input, a select box with categories and a submit button.
- Define a constant with a collection containing the book categories:
["Action", "Biography", "History", "Horror", "Kids", "Learning", "Sci-Fi"]
.
- Define the Redux actions and reducers
- Implement the
books.js
reducer withCREATE_BOOK
andREMOVE_BOOK
actions. Remember, your state can not be mutated! - Define the
CREATE_BOOK
andREMOVE_BOOK
actions inactions/index.js
. They should accept abook
object as an argument.
- Implement the
- Initialize the Redux store
- Create the initial state object in
src/index.js
. It may contain just a few books with different titles and categories. - Each book in the initial state should have a unique ID. However, we'll cheat here and implement that functionality by using the
Math.random
function. - Render the
<App/>
component wrapped in<Provider>
and pass the store down to the components.
- Create the initial state object in
Congratulations! Your bookstore manager should display some books in the table which are stored in the Redux store. Moreover, there is a form where the user might add another book to the store. Making that form functional is the goal of the next Milestone.