React Hook for accessing state and dispatch from a Redux store
Branch: master
Clone or download
ianobermiller Update comments in create.ts
move the comments to the correct code and reformat them a bit.
Latest commit f0b05c8 Feb 21, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
example Add create function for better typing Feb 9, 2019
src Update comments in create.ts Feb 22, 2019
.editorconfig init create-react-library@2.6.6 Nov 5, 2018
.gitignore Ignore coverage directory Nov 5, 2018
.prettierrc add prettier (#7) Nov 5, 2018
.travis.yml Setup travis CI Nov 5, 2018
.watchmanconfig Add tests! closes #2 Nov 5, 2018 v3.1.0 Feb 7, 2019 Create Nov 7, 2018
LICENSE init create-react-library@2.6.6 Nov 5, 2018 Add create function for better typing Feb 9, 2019
package.json v3.1.0 Feb 7, 2019
rollup.config.js Add create function for better typing Feb 9, 2019
tsconfig.json Fix warning in tsconfig Dec 27, 2018
tsconfig.test.json init create-react-library@2.6.6 Nov 5, 2018
tslint.json Switch example to TypeScript Nov 5, 2018
yarn.lock Upgrade dependencies Feb 7, 2019


React hook for accessing mapped state and dispatch from a Redux store.

Build Status NPM Bundle Size

Table of Contents


# Yarn
yarn add redux-react-hook

npm install --save redux-react-hook

Quick Start

// Bootstrap your app
import {StoreContext} from 'redux-react-hook';

  <StoreContext.Provider value={store}>
    <App />
// 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,

  // Get data from and subscribe to the store
  const {canDelete, name} = useMappedState(mapState);

  // Create actions
  const dispatch = useDispatch();
  const deleteTodo = useCallback(
    () =>
        type: 'delete todo',

  return (
    <button disabled={!canDelete} onClick={deleteTodo}>
      Delete {name}


NOTE: React hooks require react and react-dom version 16.8.0 or higher.


Before you can use the hook, you must provide your Redux store via StoreContext.Provider:

import {createStore} from 'redux';
import {StoreContext} from 'redux-react-hook';
import reducer from './reducer';

const store = createStore(reducer);

  <StoreContext.Provider value={store}>
    <App />

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:

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 <div onClick={onClick} />;


Runs the given mapState function against your store state, just like mapStateToProps.

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.

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 <li>{todo}</li>;

If you don't have any inputs (the second argument to useCallback) pass an empty array [] so React uses the same function instance each render. You could also declare mapState outside of the function, but the React team does not recommend it, since the whole point of hooks is to allow you to keep everything in the component.

NOTE: Every call to useMappedState will subscribe to the store. If the store updates, though, your component will only re-render once. So, calling useMappedState more than once (for example encapsulated inside a custom hook) should not have a large performance impact. If your measurements show a performance impact, you can switch to returning an object instead.


Simply returns the dispatch method.

import {useDispatch} from 'redux-react-hook';

function DeleteButton({index}) {
  const dispatch = useDispatch();
  const deleteTodo = useCallback(() => dispatch({type: 'delete todo', index}), [

  return <button onClick={deleteTodo}>x</button>;


Creates an instance of Redux React Hooks with a new StoreContext. The above functions are just exports of the default instance. You may want to create your own instance if:

  1. You want better type safety without annotating every callsite. Creating your own instance ensures that the types are the same for all consumers. See the example for more info.
  2. You have multiple Redux stores (this is not common)
// MyStoreHooks.js

import {create} from 'redux-react-hook';

export const {StoreContext, useDispatch, useMappedState} = create();
// MyStoreHooks.ts

import {create} from 'redux-react-hook';

// Example in TypeScript where you have defined IState and Action
export const {StoreContext, useDispatch, useMappedState} = create<
  Store<IState, Action>


You can try out redux-react-hook right in your browser with the Codesandbox example.

To run the example project locally:

# In one terminal, run `yarn start` in the root to rebuild the library itself
cd ./redux-react-example
yarn start

# In another terminal, run `yarn start` in the `example` folder
cd example
yarn start


How does this compare to React Redux?

redux-react-hook has not been battle and perf-tested, so we don't recommend replacing react-redux just yet. React Redux also guarantees that data flows top down, so that child components update after their parents, which the hook does not.

How do I fix the error "Too many re-renders. React limits the number of renders to prevent an infinite loop."

You're not memoizing the mapState function. Either declare it outside of your stateless functional component or wrap it in useCallback to avoid creating a new function every render.

More info

Hooks are really new, and we are just beginning to see what people do with them. There is an open issue on react-redux discussing the potential. Here are some other projects that are adding hooks for Redux:


Special thanks to @sawyerhood and @sophiebits for writing most of the initial implementation! This repo was setup with the help of the excellent create-react-library.


Contributions are definitely welcome! Check out the issues for ideas on where you can contribute. See the file for more details.


MIT © Facebook Inc.