https://github.com/perry-mitchell/use-reducer-state
React useReducer with state getter
https://github.com/perry-mitchell/use-reducer-state
Last synced: 11 months ago
JSON representation
React useReducer with state getter
- Host: GitHub
- URL: https://github.com/perry-mitchell/use-reducer-state
- Owner: perry-mitchell
- License: mit
- Created: 2024-02-28T10:57:49.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-28T13:06:29.000Z (over 2 years ago)
- Last Synced: 2025-08-14T12:38:40.801Z (11 months ago)
- Language: JavaScript
- Size: 44.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# useReducer state
> React `useReducer` with state getter
## About
A simple React hook `useReducer` implementation that supports state getting. Instead of just receiving the `state` and `dispatch` results from the `useReducer` hook, you receive a third option for fetching the state.
## Installation
Install by running `npm install use-reducer-state --save-dev`, and import it in your project for it to be bundled by whatever build system you're using.
## Usage
Import `useReducer` just as you would from React:
```typescript
import React from "react";
import { useReducer } from "use-reducer-state";
function initialStateGetter() {
return {
// ...
};
}
function reducerFn(state, action) {
switch (action.type) {
// ...
}
}
export function MyComponent() {
const [state, dispatch, getState] = useReducer(reducerFn, initialStateGetter);
// ...
}
```