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

File upload and Progress events with HTML5 XmlHttpRequest Level 2

The XmlHttpRequest Level 2 specification adds several enhancements to the XmlHttpRequest object. Last week I had blogged about cross-origin-requests and how it is different from Flash\Silverlight's approach .  With Level 2 specification one can upload the file to the server by passing the file object to the send method. In this post I'll try to explore uploading file using XmlHttpRequest 2 in conjunction with the progress events. I'll also provide a description on the new HTML5 tag -  progress which can be updated while the file is being uploaded to the server. And of course, some ColdFusion code that will show how the file is accepted and stored on the server directory.

Server sent events with HTML5 and ColdFusion

There are several ways to interact with the server apart from the traditional request\response and refresh all protocol. They are polling, long polling, Ajax and Websockets ( pusherapp ). Of all these Ajax and Websockets have been very popular. There is another way to interact with the server such that the server can send notifications to the client using Server Sent Events (SSE) . SSE is a part of HTML5 spec:  http://dev.w3.org/html5/eventsource/

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.