Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
count: countReducer,
person: personReducer,
});
const logger = (
{ getState }: { getState: () => State },
) => (next: Dispatch) => (action: Action) => {
/* eslint-disable no-console */
console.log('will dispatch', action);
const returnValue = next(action);
console.log('state after dispatch', getState());
/* eslint-enable no-console */
return returnValue;
};
export const { GlobalStateProvider, dispatch, useGlobalState } = createStore(
reducer,
initialState,
applyMiddleware(logger),
);
import { createStore } from 'react-hooks-global-state';
import { initialState, reducer } from './common';
export const { GlobalStateProvider, dispatch, useGlobalState } = createStore(reducer, initialState);
lastName: action.lastName,
};
case 'setAge': return {
...state,
age: action.age,
};
default: return state;
}
};
const reducer = combineReducers({
count: countReducer,
person: personReducer,
});
export const { GlobalStateProvider, dispatch, useGlobalState } = createStore(
reducer,
initialState,
compose(
applyMiddleware(reduxThunk, reduxLogger),
reduxDevToolsExt(),
),
);
import { createGlobalState, createStore } from 'react-hooks-global-state';
import { DefaultStore } from "./store";
import { Reducer } from './reducer';
const store = createStore(Reducer, DefaultStore);
export const
useStore = store.useGlobalState;
export const
StoreProvider = store.GlobalStateProvider;
export const { dispatch } = store;
age: action.age,
},
};
default: return state;
}
};
const saveStateToStorage = (
{ getState }: { getState: () => State },
) => (next: Dispatch) => (action: Action) => {
const returnValue = next(action);
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(getState()));
return returnValue;
};
export const { GlobalStateProvider, dispatch, useGlobalState } = createStore(
reducer,
initialState,
applyMiddleware(saveStateToStorage),
);
import React, { useTransition } from 'react';
import { createStore } from 'react-hooks-global-state';
import {
syncBlock,
useRegisterIncrementDispatcher,
initialState,
reducer,
ids,
useCheckTearing,
shallowEqual,
} from '../common';
const { GlobalStateProvider, dispatch, useGlobalState } = createStore(reducer, initialState);
const Counter = React.memo(() => {
const [count] = useGlobalState('count');
syncBlock();
return <div>{count}</div>;
}, shallowEqual);
const Main = () => {
const [count] = useGlobalState('count');
useCheckTearing();
useRegisterIncrementDispatcher(React.useCallback(() => {
dispatch({ type: 'increment' });
}, []));
const [localCount, localIncrement] = React.useReducer((c) => c + 1, 0);
const normalIncrement = () => {
dispatch({ type: 'increment' });