0.0.16 • Published 3 months ago

redux-rewire v0.0.16

Weekly downloads
-
License
ISC
Repository
github
Last release
3 months ago

redux-rewire

Content

Introduction

State management library for react application built on top of redux and react-redux, works well with large scale frontend applications

Following are the benefits of redux-rewire

  • Incremental store: The state for a component is only added once it is part of the rendered view
  • Immutable state: Redux-rewire automatically takes care of state mutation with the help of immer objects
  • Suitable with React Server side rendering.
  • Works well with react-native projects.
  • Full type safe: Redux-rewire is created with the intention to integrate will with large scale typescript projects

Installation

Using npm

npm install redux-rewire --save-prod

Using yarn package manager

yarn add redux-rewire -S

Basic Usage

Initialise the redux-rewire at the root of the app.

// App.js
import { configureStore, RewireProvider } from "redux-rewire";
import AppRoutes from './app/appRoutes'

function App() {
  const initialReducers = {}
  const initialState = {}
  const store = configureStore(initialReducers, initialState, {
    middlewares: [], // We can pass middlewares to the redux
  });
  return (
    <React.StrictMode>
        <RewireProvider store={store}>
          <AppRoutes />
        </RewireProvider>
    </React.StrictMode>
  );
}

export default App;

After the above initialization is done we can create build our stateful components.

Let's take an example of component which is called homeScreen.tsx

  • homeScreen.ts
  • homeScreen.reducer.ts
  • homeScreen.actions.ts
  • homeScreen.init.ts

Above the file is suffixed with .reducer, .action is just for the matter of understanding the purpose of file

Start with creating initial state for our home screen component

// Following is the content of homeScreen.init.ts

import {createInitialState }from 'redux-rewire'

interface InitialStateType { data: string[] }

export const initialState = createInitialState<InitialStateType>("home-key",{
  data: [],
});

Now, we will create the reducers for the component which will update the state Here, we are adding tha add reducers which will add the todo item in the state

Note: Do not worry about direct state mutation, internally state object is an immer object

// Following is the content of homeScreen.reducers.ts

import { createReducerSlice } from "redux-rewire";
import { initialState } from "./homeScreen.init";

// The intial state created in the homeScreen.init.ts
export const reducerSlice = createReducerSlice(initialState, {
  // state is the latest state of the home screnn component
  // actionData is the data passed while calling from home screen view
  add: (state, actionData: string) => {
    state.list.push(actionData);
    return state;
  },
});

Following file contains all the side effects or async task for the homeScreen component

For eg. we might need to send analytics to the backend when ever the todo item is added

// Following is the content of homeScreen.action.ts

import { createActionSlice } from "redux-rewire";
import {  reducerSlice } from "./homeScreen.reducer";
export const actionSlice = createActionSlice(reducerSlice, {
  add: (state, actions, actionData, key, globalState) => {
    
    // The following array returned has nothing to do with redux-rewire, it is application specific
    return [{
      actionType: "Analytics",
      payload: {
        eventNAme: "ToDOAdded",
        data: actionData
      }
    }];
  },
});

Now, in the view file we will initial our state using useRewireState api which will add the component state to redux when the view if rendered for the first time

// Following is the content of homeScreen.tsx
import {identitySelector, useGlobalState, useRewireState} from 'redux-rewire'
import {actionSlice} from './homeScreen.actions'
import {useCallback} from 'react'

const HomeScreen = (props) => {
  const [key, state, actions] = useRewireState(
    'home-key',
    actionSlice,
    identitySelector
  )
  const addToDo = useCallback(() => {
    if (inputRef.current) {
      const value = inputRef.current.value
      actions.add(value)
    }
  }, [actions])
  return (
    <div >
      <div>
        <input ref={inputRef} />
        <button onClick={addToDo}>ADD TO-DO</button>
      </div>
      <div>
        {state.list.map((v) => {
            return <div className={'item'}>Test</div>
        })}
        {state.list.length === 0 ? <div>Your To Do list is empty</div> : null}
      </div>
    </div>
  )
}

  export default HomeScreen

API Definitions

ApiinputDescriptionusage
createInitialState(compKey, intialState)
createReducerSlice(state, actionData, compKey, globalState)
createActionSlice(state, actions, actionData, key, globalState)
useRewireState(compKey, actionSlice, selectors)

Examples

Please refer the examples directory

Link to Docs

Please refer the docs directory

Advance Usage

Coming Soon

About

Redux-Rewire is developed and maintained by Fancode and many amazing contributors.

0.0.16

3 months ago

0.0.16-beta.5

5 months ago

0.0.16-beta.1

6 months ago

0.0.16-beta.0

6 months ago

0.0.16-beta.3

6 months ago

0.0.16-beta.2

6 months ago

0.0.22-beta.0

6 months ago

0.0.16-beta.4

6 months ago

0.0.15

8 months ago

0.0.10

11 months ago

0.0.11

11 months ago

0.0.12

8 months ago

0.0.13

8 months ago

0.0.14

8 months ago

0.0.1

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.9

11 months ago

0.0.8

11 months ago

0.0.5

11 months ago

0.0.4

12 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.1-beta.7

1 year ago

0.0.1-beta.6

1 year ago

0.0.1-beta.9

1 year ago

0.0.1-beta.8

1 year ago

0.0.1-beta.3

1 year ago

0.0.1-beta.2

1 year ago

0.0.1-beta.5

1 year ago

0.0.1-beta.4

1 year ago

0.0.1-beta.0

1 year ago

0.4.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago