How to use the react-hooks-global-state.createStore function in react-hooks-global-state

To help you get started, we’ve selected a few react-hooks-global-state examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dai-shi / react-hooks-global-state / examples / 07_middleware / src / state.ts View on Github external
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),
);
github dai-shi / react-hooks-global-state / examples / 09_comparison / src / state.ts View on Github external
import { createStore } from 'react-hooks-global-state';

import { initialState, reducer } from './common';

export const { GlobalStateProvider, dispatch, useGlobalState } = createStore(reducer, initialState);
github dai-shi / react-hooks-global-state / examples / 08_thunk / src / state.ts View on Github external
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(),
  ),
);
github stewartmcgown / drivestream / src / store / provider.tsx View on Github external
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;
github dai-shi / react-hooks-global-state / examples / 13_persistence / src / state.ts View on Github external
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),
);
github dai-shi / will-this-react-global-state-work-in-concurrent-mode / src / react-hooks-global-state / index.js View on Github external
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 = () =&gt; {
  const [count] = useGlobalState('count');
  useCheckTearing();
  useRegisterIncrementDispatcher(React.useCallback(() =&gt; {
    dispatch({ type: 'increment' });
  }, []));
  const [localCount, localIncrement] = React.useReducer((c) =&gt; c + 1, 0);
  const normalIncrement = () =&gt; {
    dispatch({ type: 'increment' });

react-hooks-global-state

Simple global state for React with Hooks API without Context API

MIT
Latest version published 2 years ago

Package Health Score

50 / 100
Full package analysis