Skip to main content

React Redux starter kit - Rekit

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

+ 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

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

Popular posts from this blog

Adding beforeRender and afterRender functions to a Backbone View

I was working on a Backbone application that updated the DOM when a response was received from the server. In a Backbone View, the initialize method would perform some operations and then call the render method to update the view. This worked fine, however there was scenario where in I wanted to perform some tasks before and after rendering the view. This can be considered as firing an event before and after the function had completed its execution. I found a very simple way to do this with Underscore's wrap method.

De-obfuscating javascript code in Chrome Developer Tools

I had blogged about JavaScript debugging with Chrome Developer Tools  some time back, wherein I have explained how these developer tools can help in debugging javascript code. Today Google Chrome 12 was released and my Chrome browser was updated to this version. As with every release, there have been some improvements made on performance, usability etc,. One feature that stood out for me is the ability to De-obfuscate the javascript code. What is Minification? Minification is the process of removing unnecessary characters such as white spaces, comments, new lines from the source code. These otherwise would be added to make the code more readable. Minifying the source code helps in reducing the file size and thereby reducing the time taken to download the file. This is the reason why most of the popular javascript libraries such as jQuery are minified. A minified jQuery file is of 31 KB in size where as an uncompressed one is about 229 KB. Unfortunately, debugging minified javascript f

On GraphQL and building an application using React Apollo

When I visualize building an application, I would think of using React and Redux on the front-end which talks to a set of RESTful services built with Node and Hapi (or Express). However, over a period of time, I've realized that this approach does not scale well when you add new features to the front-end. For example, consider a page that displays user information along with courses that a user has enrolled in. At a later point, you decide to add a section that displays popular book titles that one can view and purchase. If every entity is considered as a microservice then to get data from three different microservices would require three http  requests to be sent by the front-end app. The performance of the app would degrade with the increase in the number of http requests. I read about GraphQL and knew that it is an ideal way of building an app and I need not look forward to anything else. The GraphQL layer can be viewed as a facade which sits on top of your RESTful services o