1.7.14 • Published 3 days ago

react-tracked v1.7.14

Weekly downloads
6,344
License
MIT
Repository
github
Last release
3 days ago

react-tracked

Build Status npm version bundle size

Super fast React global/shared state with context and hooks

If you are looking for a Redux-based library, please visit reactive-react-redux which has the same hooks API.

Introduction

React Context and useContext is often used to avoid prop drilling, however it's known that there's a performance issue. When a context value is changed, all components that useContext will re-render. React idiomatic usage of the Context API is to separate concerns into pieces and use multiple contexts. If each context value is small enough, there shouldn't be any performance issue.

What if one wants to put a bigger state object into a context for various reasons? React Redux is one solution in this field. Redux is designed to handle one big global state, and React Redux optimizes that use case.

This library tosses a new option. It's based on Context and typically with useReducer, and provides APIs to solve the performance issue. Most notably, it comes with useTrackedState, which allows optimization without hassle. Technically, it uses Proxy underneath, and it tracks state usage in render so that if only used part of the state is changed, it will re-render.

Install

npm install react-tracked

Usage (useTracked)

import React, { useReducer } from 'react';
import ReactDOM from 'react-dom';

import { Provider, useTracked } from 'react-tracked';

const initialState = {
  count: 0,
  text: 'hello',
};

const reducer = (state, action) => {
  switch (action.type) {
    case 'increment': return { ...state, count: state.count + 1 };
    case 'decrement': return { ...state, count: state.count - 1 };
    case 'setText': return { ...state, text: action.text };
    default: throw new Error(`unknown action type: ${action.type}`);
  }
};

const useValue = () => useReducer(reducer, initialState);

const Counter = () => {
  const [state, dispatch] = useTracked();
  return (
    <div>
      {Math.random()}
      <div>
        <span>Count: {state.count}</span>
        <button type="button" onClick={() => dispatch({ type: 'increment' })}>+1</button>
        <button type="button" onClick={() => dispatch({ type: 'decrement' })}>-1</button>
      </div>
    </div>
  );
};

const TextBox = () => {
  const [state, dispatch] = useTracked();
  return (
    <div>
      {Math.random()}
      <div>
        <span>Text: {state.text}</span>
        <input value={state.text} onChange={event => dispatch({ type: 'setText', text: event.target.value })} />
      </div>
    </div>
  );
};

const App = () => (
  <Provider useValue={useValue}>
    <h1>Counter</h1>
    <Counter />
    <Counter />
    <h1>TextBox</h1>
    <TextBox />
    <TextBox />
  </Provider>
);

ReactDOM.render(<App />, document.getElementById('app'));

Technical memo

React context by nature triggers propagation of component re-rendering if a value is changed. To avoid this, this libraries use undocumented feature of calculateChangedBits. It then uses a subscription model to force update when a component needs to re-render.

API (container)

createContainer

import { createContainer } from 'react-tracked';

const useValue = () => useReducer(...); // any custom hook that returns a tuple

const {
  Provider,
  useDispatch,
  useSelector,
  useTrackedState,
  useTracked,
} = createContainer(useValue);

Provider

const App = () => (
  <Provider>
    ...
  </Provider>
);

useDispatch

const Component = () => {
  const dispatch = useDispatch(); // simply to get the second one of the tuple
  // ...
};

useSelector

const Component = () => {
  const selected = useSelector(selector); // same API in react-redux
  // ...
};

useTrackedState

const Component = () => {
  const state = useTrackedState(); // same API in reactive-react-redux
  // ...
};

useTracked

const Component = () => {
  const [state, dispatch] = useTracked(); // combination of useTrackedState and useDispatch
  // ...
};

API (default context)

Provider

import { Provider } from 'react-tracked';

const useValue = () => useReducer(...); // any custom hook that returns a tuple

const App = () => (
  <Provider useValue={useValue}>
    ...
  </Provider>
);

useDispatch

import { useDispatch } from 'react-tracked';

const Component = () => {
  const dispatch = useDispatch(); // simply to get the second one of the tuple
  // ...
};

useSelector

import { useSelector } from 'react-tracked';

const Component = () => {
  const selected = useSelector(selector); // same API in react-redux
  // ...
};

useTrackedState

import { useTrackedState } from 'react-tracked';

const Component = () => {
  const state = useTrackedState(); // same API in reactive-react-redux
  // ...
};

useTracked

import { useTracked } from 'react-tracked';

const Component = () => {
  const [state, dispatch] = useTracked(); // combination of useTrackedState and useDispatch
  // ...
};

Examples

The examples folder contains working examples. You can run one of them with

PORT=8080 npm run examples:minimal

and open http://localhost:8080 in your web browser.

You can also try them in codesandbox.io: 01 02 03 04 05 06 07

Related projects

comparison table

See #1 for details.

Blogs

2.0.0-rc.0

3 days ago

1.7.14

2 months ago

2.0.0-beta.3

2 months ago

1.7.13

2 months ago

2.0.0-beta.2

2 months ago

1.7.12

2 months ago

2.0.0-beta.1

2 months ago

1.7.11

1 year ago

1.7.10

2 years ago

1.7.9

2 years ago

1.7.8

2 years ago

1.7.7

2 years ago

1.7.6

2 years ago

1.7.5

2 years ago

1.7.4

3 years ago

1.7.3

3 years ago

1.7.2

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.6

3 years ago

1.6.5

3 years ago

1.6.4

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.6.0-beta.2

3 years ago

1.5.1

3 years ago

1.6.0-beta.1

3 years ago

1.5.0

3 years ago

2.0.0-alpha.9

3 years ago

1.4.2

4 years ago

2.0.0-alpha.8

4 years ago

2.0.0-alpha.7

4 years ago

2.0.0-alpha.5

4 years ago

2.0.0-alpha.6

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

2.0.0-alpha.4

4 years ago

2.0.0-alpha.3

4 years ago

2.0.0-alpha.2

4 years ago

2.0.0-alpha.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.11.0

5 years ago

0.10.0

5 years ago

0.9.0

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago