How to use react-hooks-global-state - 9 common examples

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 dvaJi / ReaderFront / src / state.js View on Github external
preloadImages: 3, // 0 to 20
  qualityImage: 100
};

const initialState = {
  theme: getLSItem('theme') || 'light',
  language: 'es',
  displaySettings: displaySettingsLS || displaySettings,
  coreSettings: coreSettingsLS || coreSettings
};

const {
  GlobalStateProvider,
  setGlobalState,
  useGlobalState
} = createGlobalState(initialState);

export const setTheme = theme => {
  window.localStorage.setItem('theme', theme);
  setGlobalState('theme', theme);
};

export const setLanguage = language => {
  setGlobalState('language', language);
};

export const setDisplaySettings = displaySettings => {
  setLSItem('displaySettings', displaySettings);
  setGlobalState('displaySettings', displaySettings);
};

export const setCoreSettings = coreSettings => {
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' });
github dai-shi / react-hooks-global-state / examples / 01_minimal / src / index.js View on Github external
import React, { StrictMode } from 'react';
import ReactDOM from 'react-dom';

import { createGlobalState } from 'react-hooks-global-state';

const initialState = {
  count: 0,
  text: 'hello',
};
const { GlobalStateProvider, useGlobalState } = createGlobalState(initialState);

const Counter = () =&gt; {
  const [value, update] = useGlobalState('count');
  return (
    <div>
      <span>Count: {value}</span>
      <button type="button"> update(value + 1)}&gt;+1</button>
      <button type="button"> update(value - 1)}&gt;-1</button>
    </div>
  );
};

const TextBox = () =&gt; {
  const [value, update] = useGlobalState('text');
  return (
    <div></div>
github dai-shi / react-hooks-global-state / examples / 08_thunk / src / state.ts View on Github external
};
    default: return state;
  }
};

const reducer = combineReducers({
  count: countReducer,
  person: personReducer,
});

export const { GlobalStateProvider, dispatch, useGlobalState } = createStore(
  reducer,
  initialState,
  compose(
    applyMiddleware(reduxThunk, reduxLogger),
    reduxDevToolsExt(),
  ),
);

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