export const multi = `import { useSyncExternalStore } from 'react'; export type SetState = (stateOrFn: S | ((state: S) => S)) => void; export type GetSnapshot = () => S; export type Subscribe = (listener: () => void) => () => void; export type CreateStore = | unknown[]>( initialState: T ) => { getSnapshot: GetSnapshot; setState: SetState; subscribe: Subscribe; }; export const createStore: CreateStore = < T extends Record | unknown[] >( initialState: T ) => { let state = initialState; const listeners = new Set<() => void>(); const getSnapshot = () => state; const setState: SetState = (stateOrFn) => { state = typeof stateOrFn === 'function' ? stateOrFn(state) : stateOrFn; listeners.forEach((listener) => listener()); }; const subscribe = (listener: () => void) => { listeners.add(listener); return () => { listeners.delete(listener); }; }; return { getSnapshot, setState, subscribe, }; }; export type Todo = { id: number; content: string; }[]; const initialTodo: Todo = [ { id: 0, content: 'React' }, { id: 1, content: 'Vue' }, ]; const todoStore = createStore(initialTodo); export const useTodoStore = (): [Todo, SetState] => [ useSyncExternalStore(todoStore.subscribe, todoStore.getSnapshot), todoStore.setState, ]; type Count = { count: number; info: string; }; const initialCount: Count = { count: 0, info: 'Hello', }; const countStore = createStore(initialCount); export const useCountStore = (): [Count, SetState] => [ useSyncExternalStore(countStore.subscribe, countStore.getSnapshot), countStore.setState, ];`; export const miniRedux = `import { useSyncExternalStore } from 'react'; export type RUAState = Record | unknown[]; export type RUAAction

= { payload: P; type: T; }; export type RUAReducer = ( state: S, action: A ) => S; export type RUADispatch = (action: A) => void; export type GetSnapshot = () => S; export type Subscribe = (listener: () => void) => () => void; export const createStore = ( reducer: RUAReducer, initialState: S ) => { let state = initialState; const listeners = new Set<() => void>(); const getSnapshot = () => state; const dispatch: RUADispatch = (action) => { state = reducer(state, action); listeners.forEach((listener) => listener()); }; const subscribe = (listener: () => void) => { listeners.add(listener); return () => { listeners.delete(listener); }; }; return { subscribe, getSnapshot, dispatch, }; }; export type Todo = { id: number; content: string; }[]; const initialTodo: Todo = [ { id: 0, content: 'React' }, { id: 1, content: 'Vue' }, ]; export type TodoAction = RUAAction; const reducer: RUAReducer = (state, action) => { switch (action.type) { case 'add': { if (action.payload == null) throw new Error('Add todo without payload!'); return [ ...state, { id: state[state.length - 1].id + 1, content: action.payload.toString(), }, ]; } case 'delete': { if (action.payload == null) throw new Error('Delete todo without payload!'); return state.filter((todo) => todo.id !== action.payload); } default: throw new Error('Dispatch a reducer without action!'); } }; const todoStore = createStore(reducer, initialTodo); export const useTodoStore = (): [Todo, RUADispatch] => [ useSyncExternalStore(todoStore.subscribe, todoStore.getSnapshot), todoStore.dispatch, ];`; export const MultiStore = `import Button from './Button'; import Input from './Input'; import { useState } from 'react'; import { useCountStore, useTodoStore } from './multi'; const Todo = () => { const [todos, setTodo] = useTodoStore(); const [value, setValue] = useState(''); const handleAdd = () => { if (!value) return; setTodo((d) => [...d, { id: d[d.length - 1].id + 1, content: value }]); setValue(''); }; return ( <>

); }; const Count = () => { const [{ count, info }, setState] = useCountStore(); return ( <>
Count: {count}
Info: {info}
setState((d) => ({ ...d, info: e.target.value }))} value={info} />
); }; const MultiStore = () => { return ( <>

); }; export default MultiStore;`; export const Reducer = `import Button from './Button'; import Input from './Input'; import { useState } from 'react'; import { useTodoStore } from './store.ts'; const Reducer = () => { const [todos, dispatch] = useTodoStore(); const [value, setValue] = useState(''); const handleAdd = () => { if (!value) return; dispatch({ type: 'add', payload: value, }); setValue(''); }; return ( <>
    {todos.map((todo) => (
  • {todo.content}
  • ))}
setValue(e.target.value)} />
); }; export default Reducer;`;