![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
react-sweet-state
Advanced tools
Global + local state combining the best of Redux and Context API
The good parts of Redux and React Context in a flexible, scalable and easy to use state management solution
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.
npm i react-sweet-state
# or
yarn add react-sweet-state
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 (
<div>
<h1>My counter</h1>
{state.count}
<button onClick={actions.increment}>+</button>
</div>
);
};
Check the docs website or the docs folder.
See sweet-state in action: run npm run start
and then go and check each folder:
http://localhost:8080/basic-flow/
http://localhost:8080/advanced-flow/
http://localhost:8080/advanced-scoped-flow/
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.
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.
FAQs
Global + local state combining the best of Redux and Context API
The npm package react-sweet-state receives a total of 10,693 weekly downloads. As such, react-sweet-state popularity was classified as popular.
We found that react-sweet-state demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.