DocsReact ComponentsModelProvider

ReModelProvider

The ReModelProvider component sets up the Redux store and RxJS middleware for the application. It initializes the reducers, epics, and middleware, providing a fully configured store to the React application through the Provider from react-redux.


Importing

To use the ReModelProvider, import it from @blue-functor/remodel:

import { ReModelProvider } from '@blue-functor/remodel';

Props

PropTypeRequiredDescription
childrenReact.ReactNodeYesThe React component tree that requires access to the Redux store and RxJS logic. Passed as a child to the Provider.
modelModelPropsYesObject containing reducers, epics, and optional middleware for configuring the store.
model.reducersRecord<string, Reducer>YesA mapping of reducer names to their respective reducer functions.
model.epicsRecord<string, Epic>YesA mapping of epic names to their respective epic functions, handling side effects and asynchronous workflows.
model.middlewareMiddleware[]NoAdditional Redux middleware to include in the store.
optionsRecord<string, any>NoA generic object for providing additional dependencies to epics. These options are passed to the epics via the dependencies argument in createEpicMiddleware.
initialStateRecord<string, any>NoThe initial state of the Redux store (default: {}). Use this to prepopulate the store with data at initialization.
persistStatebooleanNoDetermines whether the state should be persisted. (This parameter is included for potential future enhancements but is not yet implemented in the provided code.)

Example Usage

src/App.tsx
import React from 'react';
import { ReModelProvider } from '@blue-functor/remodel';
import model from './models';
 
const App = () => {
  return (
    <ReModelProvider model={model} initialState={{ user: { loggedIn: true } }}>
      <div>
        <h1>Welcome to ReModel</h1>
      </div>
    </ReModelProvider>
  );
};
 
export default App;

How It Works

  1. Redux Store Initialization:
  • Combines model.reducers with the effect reducer using combineReducers.
  • Configures createEpicMiddleware with dependencies, including options and a custom dispatch method.
  1. Epic Management:
  • The epicManager initializes and manages the provided model.epics.
  • Executes the root epic with the epicMiddleware.
  1. Middleware Integration:
  • Combines epicMiddleware with any additional middleware from model.middleware.
  1. Context Provision:
  • Provides the fully configured Redux store to the React component tree using the Provider from react-redux.

System Epics

The ReModelProvider includes system epics that handle the following functionalities:

  • Model Injection: Dynamically injects new models (reducers and epics) into the store as needed.
  • Loading Tracking: Automatically tracks the lifecycle of effect actions, making it easier to manage and monitor loading states.

This component streamlines the integration of Redux and RxJS, offering a scalable, modular solution for state and side effect management, with built-in enhancements for dynamic model management and effect-state tracking.