In this post, I will show you the fast way to fire up an React app. Of course, we will use CRA, several awesome libraries such as redux-toolkit, react-redux, axios and so.
1. Start up
Let’s find a good directory and fire up the following command:
2. Dependencies
You will need checking .gitignore and add in anything that you don’t want to include. Run either one of the commands below to install the necessary dependencies:
Open package.json and verify if following libraries exist under dependencies field (versions might not be the same):
3. Project Structure
I’m lazy, so I use the same approach for every app to save time and energy. Within the src directory, create following directories:
Folder components is for React components in various levels: layout, sidebar, main content, … While store is for Redux stuff.
4. Redux toolkit
Redux toolkit eases the pain of repetitive coding style required by Redux (in Javascript). You will be surprised that we don’t have any reducer/action file/folder, instead, we only have things called Slice. It’s all you need.
You would need to create a store.js file in store folder to gather all the slices available in your app.
5. Network middleware
In order to have a solid API call workflow, we have to setup a middleware file which I called api.js in folder store/middleware.
6. Environment variables
In the root folder, you can create .env and .env.development to set environment variables. Remember that every ENV that is used by CRA must be prefixed with REACT_APP_ and used with process.env.REACT_APP_.