Redux-Bundler Create React App Template

1.0.0 · maintenance · verified Tue Apr 21

cra-template-redux-bundler provides a starting point for building React applications using Create React App with redux-bundler integrated. Redux-bundler, currently at version 29.1.0, is a state management library designed to simplify Redux applications by organizing logic into modular "bundles." It promotes a feature-centric architecture, moving away from the traditional Redux separation of actions, reducers, and selectors into separate directories. Key features include a "batteries included" philosophy with optional routing and data fetching capabilities, a "reactor" pattern for declarative state reactions, and built-in support for code-splitting and lazy-loading of state logic. This approach significantly reduces boilerplate and aims to improve maintainability and scalability for complex applications, particularly Progressive Web Apps (PWAs) that benefit from small bundle sizes and network resilience. The template itself is at version 1.0.0 and streamlines the initial setup process, configuring the necessary redux-bundler infrastructure within a standard Create React App project.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing a redux-bundler store with a simple user bundle and connecting a React component using `redux-bundler-react` to display user status and trigger actions.

import React from 'react';
import ReactDOM from 'react-dom/client';
import { createStore, composeBundles } from 'redux-bundler';
import { Provider } from 'react-redux'; // Often used with redux-bundler
import { connect } from 'redux-bundler-react';

const userBundle = {
  name: 'user',
  reducer: (state = { name: 'Guest', loggedIn: false }, action) => {
    if (action.type === 'USER_LOGIN_SUCCESS') {
      return { ...state, name: action.payload.name, loggedIn: true };
    }
    if (action.type === 'USER_LOGOUT') {
      return { ...state, name: 'Guest', loggedIn: false };
    }
    return state;
  },
  selectUserName: (state) => state.user.name,
  selectIsLoggedIn: (state) => state.user.loggedIn,
  doLogin: (name) => ({ dispatch }) => {
    // Simulate API call
    setTimeout(() => {
      dispatch({ type: 'USER_LOGIN_SUCCESS', payload: { name } });
    }, 500);
  },
  doLogout: () => ({ dispatch }) => {
    dispatch({ type: 'USER_LOGOUT' });
  },
};

const appBundles = composeBundles(userBundle);
const store = createStore(appBundles);

function MyComponent({ userName, isLoggedIn, doLogin, doLogout }) {
  return (
    <div>
      <h1>Hello, {userName}!</h1>
      {isLoggedIn ? (
        <button onClick={doLogout}>Logout</button>
      ) : (
        <button onClick={() => doLogin('Alice')}>Login as Alice</button>
      )}
    </div>
  );
}

const ConnectedMyComponent = connect(
  'selectUserName',
  'selectIsLoggedIn',
  'doLogin',
  'doLogout',
)(MyComponent);

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <Provider store={store}>
    <ConnectedMyComponent />
  </Provider>
);

view raw JSON →