# redux-react-hook
**Repository Path**: mirrors_gaearon/redux-react-hook
## Basic Information
- **Project Name**: redux-react-hook
- **Description**: React Hook for accessing state and dispatch from a Redux store
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-08-08
- **Last Updated**: 2026-05-23
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# redux-react-hook
> React hook for accessing mapped state and dispatch from a Redux store.
[](https://travis-ci.com/facebookincubator/redux-react-hook)
[](https://www.npmjs.com/package/redux-react-hook)
[](https://bundlephobia.com/result?p=redux-react-hook@latest)
## Table of Contents
- [Install](#install)
- [Quick Start](#quick-start)
- [Usage](#usage)
- [`StoreContext`](#storecontext)
- [`useMappedState(mapState)`](#usemappedstatemapstate)
- [`useDispatch()`](#usedispatch)
- [Example](#example)
- [FAQ](#faq)
- [More info](#more-info)
- [Thanks](#thanks)
- [Contributing](#contributing)
- [License](#license)
## Install
```bash
# Yarn
yarn add redux-react-hook
# NPM
npm install --save redux-react-hook
```
## Quick Start
```tsx
//
// Bootstrap your app
//
import {StoreContext} from 'redux-react-hook';
ReactDOM.render(
,
document.getElementById('root'),
);
```
```tsx
//
// Individual components
//
import {useDispatch, useMappedState} from 'redux-react-hook';
export function DeleteButton({index}) {
// Declare your memoized mapState function
const mapState = useCallback(
state => ({
canDelete: state.todos[index].canDelete,
name: state.todos[index].name,
}),
[index],
);
// Get data from and subscribe to the store
const {canDelete, name} = useMappedState(mapState);
// Create actions
const dispatch = useDispatch();
const deleteTodo = useCallback(
() =>
dispatch({
type: 'delete todo',
index,
}),
[index],
);
return (
);
}
```
## Usage
NOTE: React hooks currently require `react` and `react-dom` version `16.7.0-alpha.0` or higher.
### `StoreContext`
Before you can use the hook, you must provide your Redux store via `StoreContext.Provider`:
```tsx
import {createStore} from 'redux';
import {StoreContext} from 'redux-react-hook';
import reducer from './reducer';
const store = createStore(reducer);
ReactDOM.render(
,
document.getElementById('root'),
);
```
You can also use the `StoreContext` to access the store directly, which is useful for event handlers that only need more state when they are triggered:
```tsx
import {useContext} from 'react';
import {StoreContext} from 'redux-react-hook';
function Component() {
const store = useContext(StoreContext);
const onClick = useCallback(() => {
const value = selectExpensiveValue(store.getState());
alert('Value: ' + value);
});
return
;
}
```
### `useMappedState(mapState)`
Runs the given `mapState` function against your store state, just like
`mapStateToProps`.
```tsx
const state = useMappedState(mapState);
```
You can use props or other component state in your `mapState` function. It must be memoized with `useCallback`, because `useMappedState` will infinitely recurse if you pass in a new mapState function every time.
```tsx
import {useMappedState} from 'redux-react-hook';
function TodoItem({index}) {
// Note that we pass the index as a dependency parameter -- this causes
// useCallback to return the same function every time unless index changes.
const mapState = useCallback(state => state.todos[index], [index]);
const todo = useMappedState(mapState);
return