I have been developing applications using React and Redux for quite some time now and I feel there are several starter kits out there. Although some add too much of boilerplate code, some include several libraries (to make it one kit that includes all) and some take the route of adding minimal boilerplate to include only the required libraries. I plan to write about these React-Redux starter kits/boilerplates in the coming weeks. This post focuses on a starter kit called Rekit. Rekit provides basic scaffolding and comes with a CLI that allows you to add features to your React application. Rekit focuses on application structure. It divides the application in terms of features, wherein each feature acts as a decoupled component and then assembled at the root level.
Since I mentioned the word 'boilerplate', a React-Redux application would have to include some boilerplate code before one starts to write any functional code. Even a simple application would need some boilerplate code added. A project created with Rekit has basic scaffolding and also includes boilerplate code such as configuring a store, adding middleware such as redux-thunk and router (react-router-redux), adding root path (/) to the list of routes and defining an App container where the route components would be rendered etc. It brings developer to a place where he's are ready to add functional code to the application.
Rekit follows feature oriented architecture to structure the application. Instead of building the application in terms of containers, components and actions; an application built with Rekit is organized in terms of features. Here, each feature is decoupled and would have its own set of containers, components, routes and actions. For example, consider an application that allows you to add a to-do and list the to-dos; it would have two routes - /add and /list, two containers - 'Add' and 'List' that connect to the Redux store and dispatch actions that update the state variables in the Redux store, actions - 'addTodo' and 'getTodos' that update and read from the Redux state. Also, the CLI allows you to create dumb components(view-only) that are then included in container's render method whose sole responsibility would be to display the values passed to it and invoke handlers defined in its parent container.
Rekit CLI makes it really easy to create an application and add features to it. After installing Rekit (npm i -g rekit), you can use rekit command to create a project.
rekit create todo-app
this would create a todo-app with package.json in it. Change the directory to the newly created project and then do 'npm i' to install the dependencies.
The next step would be to add a feature and a couple of Containers and Actions in the feature:
rekit add feature todo
rekit add component todo/List -c // -c flag to create a container component
rekit add component todo/Add -c
rekit add action todo/add
Rekit scaffolds the project with a basic setup which includes the entry point to the application in index.js, redux store and route configuration related files in common directory and it also includes a 'default page' as a feature (not shown above). It adds/updates directories and files in the project as and when you add a feature, component or an action.
On one action per file methodology:
Rekit makes it a mandate to create one action per file instead of several actions in a single file and calling it as a reducer. I was initially not comfortable with this approach but then by following through the application structure, it made more sense. Every action is defined in a file and it exports two functions – an action and a reducer.
The CLI then includes this file in feature’s root reducer file - todo/redux/reducer.js:
Notice the last line - return reducers.reduce((s, r) => r(s, action), newState); it returns the reducer function for each of the reducers defined in the feature. The feature root reducer was earlier imported into the application’s root reducer:
The CLI adds the import statements, updates the reducer list and keeps the application updated whenever a new feature is added or updated. I've been very happy with this starter kit so far, not to forget, it adds unit test files, linting rules and build configuration files. Also, there is an option to replace redux-thunk with redux-saga.
I've created a sample todo app and have posted the code on GitHub - https://github.com/sagar-ganatra/rekit-todo-app
Since I mentioned the word 'boilerplate', a React-Redux application would have to include some boilerplate code before one starts to write any functional code. Even a simple application would need some boilerplate code added. A project created with Rekit has basic scaffolding and also includes boilerplate code such as configuring a store, adding middleware such as redux-thunk and router (react-router-redux), adding root path (/) to the list of routes and defining an App container where the route components would be rendered etc. It brings developer to a place where he's are ready to add functional code to the application.
Rekit follows feature oriented architecture to structure the application. Instead of building the application in terms of containers, components and actions; an application built with Rekit is organized in terms of features. Here, each feature is decoupled and would have its own set of containers, components, routes and actions. For example, consider an application that allows you to add a to-do and list the to-dos; it would have two routes - /add and /list, two containers - 'Add' and 'List' that connect to the Redux store and dispatch actions that update the state variables in the Redux store, actions - 'addTodo' and 'getTodos' that update and read from the Redux state. Also, the CLI allows you to create dumb components(view-only) that are then included in container's render method whose sole responsibility would be to display the values passed to it and invoke handlers defined in its parent container.
Rekit CLI makes it really easy to create an application and add features to it. After installing Rekit (npm i -g rekit), you can use rekit command to create a project.
rekit create todo-app
this would create a todo-app with package.json in it. Change the directory to the newly created project and then do 'npm i' to install the dependencies.
The next step would be to add a feature and a couple of Containers and Actions in the feature:
rekit add feature todo
rekit add component todo/List -c // -c flag to create a container component
rekit add component todo/Add -c
rekit add action todo/add
+ todo-app
+ src
+ common
- configStore.js
- history.js
- rootReducer.js
- routeConfig.js
+ features
+ todo
+ redux
- actions.js
- add.js
- constants.js
- initialState.js
- reducer.js
- update.js
- Add.js
- Add.less
- index.js
- List.js
- List.less
- ListItem.js
- ListItem.less
- route.js
- style.less
+ images
+ styles
- index.html
- index.js
- Root.js
+ tests
+ tools
+ src
+ common
- configStore.js
- history.js
- rootReducer.js
- routeConfig.js
+ features
+ todo
+ redux
- actions.js
- add.js
- constants.js
- initialState.js
- reducer.js
- update.js
- Add.js
- Add.less
- index.js
- List.js
- List.less
- ListItem.js
- ListItem.less
- route.js
- style.less
+ images
+ styles
- index.html
- index.js
- Root.js
+ tests
+ tools
Rekit scaffolds the project with a basic setup which includes the entry point to the application in index.js, redux store and route configuration related files in common directory and it also includes a 'default page' as a feature (not shown above). It adds/updates directories and files in the project as and when you add a feature, component or an action.
On one action per file methodology:
Rekit makes it a mandate to create one action per file instead of several actions in a single file and calling it as a reducer. I was initially not comfortable with this approach but then by following through the application structure, it made more sense. Every action is defined in a file and it exports two functions – an action and a reducer.
The CLI then includes this file in feature’s root reducer file - todo/redux/reducer.js:
Notice the last line - return reducers.reduce((s, r) => r(s, action), newState); it returns the reducer function for each of the reducers defined in the feature. The feature root reducer was earlier imported into the application’s root reducer:
The CLI adds the import statements, updates the reducer list and keeps the application updated whenever a new feature is added or updated. I've been very happy with this starter kit so far, not to forget, it adds unit test files, linting rules and build configuration files. Also, there is an option to replace redux-thunk with redux-saga.
I've created a sample todo app and have posted the code on GitHub - https://github.com/sagar-ganatra/rekit-todo-app
Comments
Post a Comment