MCPcopy Index your code
hub / github.com/atlassian/react-sweet-state

github.com/atlassian/react-sweet-state @v2.7.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.7.2 ↗ · + Follow
117 symbols 335 edges 72 files 2 documented · 2%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

react-sweet-state logo

react-sweet-state

The good parts of Redux and React Context in a flexible, scalable and easy to use state management solution

Philosophy

sweet-state is heavily inspired by Redux mixed with Context API concepts. It has render-prop components or hooks, connected to Store instances (defined as actions and initial state), receiving the Store state (or part of it) and the actions as a result.

Each Hook, or Subscriber, is responsible to get the instantiated Store (creating a new one with initialState if necessary), allowing sharing state across your project extremely easy.

Similar to Redux thunks, actions receive a set of arguments to get and mutate the state. The default setState implementation is similar to React setState, accepting an object that will be shallow merged with the current state. However, you are free to replace the built-in setState logic with a custom mutator implementation, like immer for instance.

Basic usage

npm i react-sweet-state
# or
yarn add react-sweet-state

Creating and consuming stores

import { createStore, createHook } from 'react-sweet-state';

const Store = createStore({
  // value of the store on initialisation
  initialState: {
    count: 0,
  },
  // actions that trigger store mutation
  actions: {
    increment:
      () =>
      ({ setState, getState }) => {
        // mutate state synchronously
        setState({
          count: getState().count + 1,
        });
      },
  },
  // optional, unique, mostly used for easy debugging
  name: 'counter',
});

const useCounter = createHook(Store);
// app.js
import { useCounter } from './components/counter';

const CounterApp = () => {
  const [state, actions] = useCounter();
  return (



      <h1>My counter</h1>
      {state.count}
      <button onClick={actions.increment}>+</button>



  );
};

Documentation

Check the docs website or the docs folder.

Examples

See sweet-state in action: run npm run start and then go and check each folder:

  • Basic example with Flow typing http://localhost:8080/basic-flow/
  • Advanced async example with Flow typing http://localhost:8080/advanced-flow/
  • Advanced scoped example with Flow typing http://localhost:8080/advanced-scoped-flow/

Contributing

To test your changes you can run the examples (with npm run start). Also, make sure you run npm run preversion before creating you PR so you will double check that linting, types and tests are fine.

Thanks

This library merges ideas from redux, react-redux, redux-thunk, react-copy-write, unstated, bey, react-apollo just to name a few. Moreover it has been the result of months of discussions with ferborva, pksjce, TimeRaider, dpisani, JedWatson, and other devs at Atlassian.

With ❤️ from Atlassian

Extension points exported contracts — how you extend this code

SetState (Interface)
(no doc)
types/index.d.ts
StoreInstance (Interface)
(no doc)
types/index.d.ts
GenericContainerComponent (Interface)
(no doc)
types/index.d.ts
OverrideContainerComponent (Interface)
(no doc)
types/index.d.ts

Core symbols most depended-on inside this repo

createSubscriber
called by 47
src/components/subscriber.js
setState
called by 43
src/store/create-state.js
createStore
called by 35
src/store/create.js
createHook
called by 33
src/components/hook.js
createContainer
called by 25
src/components/container.js
shallowEqual
called by 24
src/utils/shallow-equal.js
getState
called by 19
src/store/create-state.js
createMemoizedSelector
called by 11
src/utils/create-selector.js

Shape

Function 102
Class 8
Interface 4
Method 3

Languages

TypeScript100%

Modules by API surface

src/store/create-state.js7 symbols
src/components/container.js7 symbols
types/index.d.ts6 symbols
src/components/__tests__/hook.test.js6 symbols
src/__tests__/integration.test.js6 symbols
examples/performance-test/views/tree.js6 symbols
src/store/bind-actions.js5 symbols
src/components/hook.js5 symbols
src/store/registry.js4 symbols
src/components/__tests__/subscriber.test.js4 symbols
src/utils/memoize.js3 symbols
src/utils/create-selector.js3 symbols

For agents

$ claude mcp add react-sweet-state \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact