### Constate Setup Phase Example Source: https://github.com/diegohaz/constate/blob/main/_autodocs/implementation.md Demonstrates the initial setup phase of Constate, where a custom hook is passed along with selectors to the constate function. This results in the creation of a Provider component and custom hooks. ```typescript function useCounter() { const [count, setCount] = useState(0); return { count, setCount }; } const [Provider, useCount, useSetCount] = constate( useCounter, (value) => value.count, (value) => value.setCount, ); ``` -------------------------------- ### Plain Context API Example Source: https://github.com/diegohaz/constate/blob/main/_autodocs/migration-and-comparison.md Illustrates the traditional React Context API setup for managing state, including context creation, a custom hook for consumption, and a provider component. ```typescript const CountContext = React.createContext( undefined ); function useCountContext() { const context = React.useContext(CountContext); if (!context) { throw new Error("useCountContext must be used within CountProvider"); } return context; } function CountProvider({ children }: { children: React.ReactNode }) { const [count, setCount] = useState(0); const increment = useCallback(() => setCount((c) => c + 1), []); const value = useMemo(() => ({ count, setCount, increment }), [count]); return ( {children} ); } ``` -------------------------------- ### Constate Installation Source: https://github.com/diegohaz/constate/blob/main/_autodocs/README.md Command to install the constate library using npm. Ensure you have Node.js and npm installed. ```bash npm install constate ``` -------------------------------- ### Install ConstaTe Source: https://github.com/diegohaz/constate/blob/main/_autodocs/quick-reference.md Install the ConstaTe package using npm, yarn, or pnpm. ```bash npm install constate # or yarn add constate # or pnpm add constate ``` -------------------------------- ### Install Constate with pnpm Source: https://github.com/diegohaz/constate/blob/main/README.md Install the constate package using pnpm. ```sh pnpm add constate ``` -------------------------------- ### Install Constate with Yarn Source: https://github.com/diegohaz/constate/blob/main/README.md Install the constate package using Yarn. ```sh yarn add constate ``` -------------------------------- ### Install Constate with npm Source: https://github.com/diegohaz/constate/blob/main/README.md Install the constate package using npm. ```sh npm i constate ``` -------------------------------- ### Redux Example for Counter Source: https://github.com/diegohaz/constate/blob/main/_autodocs/migration-and-comparison.md Presents a typical Redux implementation for a counter, including action types, reducer, store setup, and component usage with hooks. ```javascript // Action types const INCREMENT = "INCREMENT"; // Reducer function counterReducer(state = 0, action) { switch (action.type) { case INCREMENT: return state + 1; default: return state; } } // Store setup const store = createStore(counterReducer); // Component function Counter() { const count = useSelector((state) => state); const dispatch = useDispatch(); return ; } ``` -------------------------------- ### Vitest Setup for Counter Provider Source: https://github.com/diegohaz/constate/blob/main/_autodocs/testing-guide.md Demonstrates a typical Vitest setup for testing a Counter Provider. It includes necessary imports, describe blocks, and beforeEach hooks for mock clearing. ```typescript import { describe, test, expect, vi, beforeEach, afterEach } from "vitest"; import { render, renderHook, fireEvent, act } from "@testing-library/react"; describe("Counter Provider", () => { beforeEach(() => { vi.clearAllMocks(); }); test("provides context to components", () => { const { result } = renderHook(() => useCounterContext(), { wrapper: CounterProvider, }); expect(result.current.count).toBe(0); }); test("increments count", () => { const { result } = renderHook(() => useCounterContext(), { wrapper: CounterProvider, }); act(() => { result.current.increment(); }); expect(result.current.count).toBe(1); }); }); ``` -------------------------------- ### Example File Structure Source: https://github.com/diegohaz/constate/blob/main/_autodocs/quick-reference.md Illustrates a common project structure for organizing context-based state management with Consta, separating concerns into distinct context directories. ```tree src/ ├── contexts/ │ ├── auth/ │ │ ├── useAuth.ts │ │ └── AuthProvider.tsx │ ├── theme/ │ │ ├── useTheme.ts │ │ └── ThemeProvider.tsx │ └── index.ts └── App.tsx ``` -------------------------------- ### Basic Constate Usage Example Source: https://github.com/diegohaz/constate/blob/main/_autodocs/api-reference/provider.md A fundamental example of using constate to create a Provider and a hook for managing a simple counter state. ```typescript const [Provider, useValue] = constate(() => { const [count, setCount] = useState(0); return { count, setCount }; }); function App() { return (