Skip to content

Instantly share code, notes, and snippets.

@cmrigney
Created April 11, 2019 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmrigney/de7f485866c8cf140e4a046ee3305676 to your computer and use it in GitHub Desktop.
Save cmrigney/de7f485866c8cf140e4a046ee3305676 to your computer and use it in GitHub Desktop.
Example for useRedux
// Adapted code from https://github.com/flepretre/use-redux
import { useContext, useState, useEffect, useCallback } from 'react';
import { ReactReduxContext } from 'react-redux';
import { bindActionCreators, ActionCreator } from 'redux';
export function useRedux() {
const { store } = useContext(ReactReduxContext);
const { getState, dispatch, subscribe } = store;
const reduxState = getState();
const [state, setState] = useState(reduxState);
const updateState = () => {
setState(getState());
};
useEffect(() => subscribe(updateState), [state]);
return [state, dispatch];
}
export function useSelector<T extends (state: any) => any>(selector: T): ReturnType<T> {
const { store } = useContext(ReactReduxContext);
const { getState, subscribe } = store;
const [value, setValue] = useState(() => selector(getState()));
const updateState = () => {
const newValue = selector(getState());
if (newValue !== value) {
setValue(newValue);
}
};
useEffect(() => subscribe(updateState), [value]);
return value;
}
export function useActionCreator<A, T extends ActionCreator<A>>(actionCreator: T): T {
const { store } = useContext(ReactReduxContext);
const { dispatch } = store;
return useCallback(bindActionCreators(actionCreator, dispatch), [actionCreator]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment