Overview of poly-state state management library
poly-state is a boilerplate-free state management library designed for use with React, Next.js, Preact, and SolidJS applications.
website·Indexed 29 days ago
https://poly-state.github.ioA boilerplate-free state management library for React, Next.js, Preact, and SolidJS applications. It features a fully typed API without reducers or actions, support for Redux DevTools, and specialized hooks via createStoreHooks.
Poly-state manages state through a store-and-hook pattern that does not require a Provider wrapper.
createStore function accepts an initial state and returns an instance of an internal Store class along with setter methods for each property in the state.useStore hook subscribes to state updates. When the internal state changes, the hook updates the returned value, triggering a UI re-render. The hook automatically handles cleanup on component unmount.Redux DevTools can be installed across various environments:
electron-devtools-installer and specify REDUX_DEVTOOLS.remote-redux-devtools..use() method on the store instance.const todoStore = new TodoStore(initialState);
todoStore
.use(loggerMiddleware) // Add the logger middleware
.use(anotherMiddleware); // You can use multiple middlewares# Using npm
npm install @poly-state/poly-state
# Using yarn
yarn add @poly-state/poly-stateasync methods within a store class. Once the asynchronous operation completes and the data is retrieved, call this.setState() to update the store with the result.class TodoStore extends getStoreClass<TodoStoreState>() {
async addAsyncTodo(text: string) {
const response = await fetch('/api/todos', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text }),
});
const todo = (await response.json()) as Todo;
if (response.ok) {
this.setState((state) => ({
...state,
todos: [...state.todos, todo],
count: state.count + 1,
}));
}
}
}withDevTools middleware. The second argument to withDevTools serves as the store identifier within the Redux DevTools interface.import { createStore, withDevTools } from '@poly-state/poly-state';
import { useStore } from '@poly-state/react';
const counterStore = createStore({ count: 0 });
withDevTools(counterStore, 'Counter Store');
const useCounterStore = () => useStore(counterStore);@poly-state/react package using npm or yarn.# Using npm
npm install @poly-state/react
# Using yarn
yarn add @poly-state/reactwithDevTools call in a conditional check using process.env.NODE_ENV.import { createStore, withDevTools } from '@poly-state/poly-state';
const counterStore = createStore({ count: 0 });
if (process.env.NODE_ENV === 'development') {
withDevTools(counterStore, 'Counter Store');
}createStoreHooks function takes a store instance and returns a tuple containing two custom hooks: UseStore (to access the entire store state) and UseStoreSelector (to access a specific key from the store state). This is the recommended way to create typed, reusable hooks for a specific store instance instead of calling the generic hooks repeatedly.import { createStore, createStoreHooks } from '@poly-state/react';
const counterStore = createStore({ count: 0 });
const [useCounterStore, useCounterSelector] = createStoreHooks(counterStore);
// Usage in a component:
function Component() {
const { count } = useCounterStore(); // Access full state
const countValue = useCounterSelector('count'); // Access specific key
return <div>{countValue}</div>;
}yarn add @poly-state/poly-state @poly-state/reactcreateStore from @poly-state/poly-state to define the initial state and createStoreHooks from @poly-state/react to generate a hook for accessing that state within React components. createStoreHooks returns a tuple containing a store hook and a selector hook.import { createStore } from '@poly-state/poly-state';
import { createStoreHooks } from '@poly-state/react';
const counterStore = createStore({ count: 0 });
const [useCounterStore, useCounterSelector] = createStoreHooks(counterStore);