How to use the easy-peasy.createTypedHooks function in easy-peasy

To help you get started, we’ve selected a few easy-peasy 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 jamaljsr / polar / src / store / index.ts View on Github external
};

// using injections allows for more easily mocking of dependencies in store actions
// see https://easy-peasy.now.sh/docs/testing/testing-components.html#mocking-calls-to-services
const injections: StoreInjections = {
  ipc: createIpcSender('AppModel', 'app'),
  settingsService,
  dockerService,
  bitcoindService,
  lightningFactory: new LightningFactory(),
};

const store = createReduxStore({ injections });

// export hooks directly from the store to get proper type inference
const typedHooks = createTypedHooks();
export const { useStoreActions, useStoreDispatch, useStoreState } = typedHooks;

// enable hot reload for models
if (process.env.NODE_ENV !== 'production') {
  if ((module as any).hot) {
    (module as any).hot.accept('./models', () => {
      const models = createModel(hashHistory);
      store.reconfigure(models);
    });
  }
}

export default store;
github ctrlplusb / easy-peasy / src / __tests__ / typescript / hooks.ts View on Github external
}

let dispatch = useStoreDispatch();
dispatch({ type: 'FOO' });

let useStoreResult = useStoreState((state: State) => state.stateNumber);
useStoreResult + 1;
let useActionResult = useStoreActions(
  (actions: Actions) => actions.actionImp,
);
useActionResult(1);

let store = useStore();
store.getState().stateString + 'world';

const typedHooks = createTypedHooks();

useStoreResult = typedHooks.useStoreState(state => state.stateNumber);
useStoreResult + 1;
useActionResult = typedHooks.useStoreActions(actions => actions.actionImp);
useActionResult(1);
dispatch = typedHooks.useStoreDispatch();
dispatch({
  type: 'FOO',
});
store = typedHooks.useStore();
store.getState().stateString + 'world';

let actionNoPayload = typedHooks.useStoreActions(
  actions => actions.actionNoPayload,
);
actionNoPayload();
github ctrlplusb / easy-peasy / src / __tests__ / typescript / implementation.tsx View on Github external
document.createElement('div'),
);

/**
 * We also support typing react-redux
 */

const Counter: React.SFC<{ counter: number }> = ({ counter }) => (
  <div>{counter}</div>
);

connect((state: State) =&gt; ({
  counter: state.counter,
}))(Counter);

const typedHooks = createTypedHooks();

typedHooks.useAction(actions =&gt; actions.todos.addTodo)('bar');
typedHooks.useActions(actions =&gt; actions.todos.addTodo)('bar');
typedHooks.useStore(state =&gt; state.todos.items).concat(['what']);
typedHooks.useDispatch().todos.addTodo('foo');