React hook for accessing mapped state from a Redux store.
# Yarn
yarn add redux-react-hook
# NPM
npm install --save redux-react-hook
NOTE: React hooks currently require react
and react-dom
version 16.7.0-alpha.0
or higher.
In order to use the hooks, your Redux store must be in available in the React context from StoreProvider
.
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);
ReactDOM.render(
<StoreContext.Provider value={store}>
<App />
</StoreContext.Provider>,
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:
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);
If your mapState
function doesn't use props or other component state,
declare it outside of your stateless functional component:
import {useMappedState} from 'redux-react-hook';
// Note how mapState is declared outside of the function -- this is critical, as
// useMappedState will infinitely recurse if you pass in a new mapState
// function every time.
const mapState = state => ({
lastUpdated: state.lastUpdated,
todoCount: state.todos.length,
});
export default function TodoSummary() {
const {lastUpdated, todoCount} = useMappedState(mapState);
return (
<div>
<div>Count: {todoCount}</div>
<div>Last updated: {new Date(lastUpdated).toString()}</div>
</div>
);
}
If you need to use props or other component state in your mapState
function,
memoize the function with useCallback
:
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>;
}
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}), [
index,
]);
return <button onClick={deleteTodo}>x</button>;
}
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 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.
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 hook! 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 CONTRIBUTING.md file for more details.
MIT © Facebook Inc.