How to use use-context-selector - 10 common examples

To help you get started, we’ve selected a few use-context-selector 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 / will-this-react-global-state-work-in-concurrent-mode / src / use-context-selector / index.js View on Github external
const Main = () => {
  const dispatch = useContextSelector(context, (v) => v[1]);
  const count = useContextSelector(context, (v) => v[0].count);
  useCheckTearing();
  useRegisterIncrementDispatcher(React.useCallback(() => {
    dispatch({ type: 'increment' });
  }, [dispatch]));
  const [localCount, localIncrement] = React.useReducer((c) => c + 1, 0);
  const normalIncrement = () => {
    dispatch({ type: 'increment' });
  };
  const [startTransition, isPending] = useTransition();
  const transitionIncrement = () => {
    startTransition(() => {
      dispatch({ type: 'increment' });
    });
  };
  return (
github dai-shi / use-context-selector / examples / 01_minimal / src / index.js View on Github external
const Counter = () => {
  const count = useContextSelector(context, (v) => v[0].count);
  const dispatch = useContextSelector(context, (v) => v[1]);
  return (
    <div>
      {Math.random()}
      <div>
        <span>Count: {count}</span>
        <button type="button"> dispatch({ type: 'increment' })}&gt;+1</button>
        <button type="button"> dispatch({ type: 'decrement' })}&gt;-1</button>
      </div>
    </div>
  );
};
github dai-shi / use-context-selector / examples / 01_minimal / src / index.js View on Github external
const TextBox = () =&gt; {
  const text = useContextSelector(context, (v) =&gt; v[0].text);
  const dispatch = useContextSelector(context, (v) =&gt; v[1]);
  return (
    <div>
      {Math.random()}
      <div>
        <span>Text: {text}</span>
        <input value="{text}"> dispatch({ type: 'setText', text: event.target.value })} /&gt;
      </div>
    </div>
  );
};
github dai-shi / use-context-selector / examples / 02_typescript / src / Counter.tsx View on Github external
const Counter = () =&gt; {
  const count = useContextSelector(MyContext, (v) =&gt; v[0].count);
  const dispatch = useContextSelector(MyContext, (v) =&gt; v[1]);
  return (
    <div>
      {Math.random()}
      <div>
        <span>Count: {count}</span>
        <button type="button"> dispatch({ type: 'increment' })}&gt;+1</button>
        <button type="button"> dispatch({ type: 'decrement' })}&gt;-1</button>
      </div>
    </div>
  );
};
github dai-shi / will-this-react-global-state-work-in-concurrent-mode / src / use-context-selector / index.js View on Github external
const Counter = React.memo(() =&gt; {
  const count = useContextSelector(context, (v) =&gt; v[0].count);
  syncBlock();
  return <div>{count}</div>;
}, shallowEqual);
github dai-shi / use-context-selector / examples / 02_typescript / src / Counter.tsx View on Github external
const Counter = () =&gt; {
  const count = useContextSelector(MyContext, (v) =&gt; v[0].count);
  const dispatch = useContextSelector(MyContext, (v) =&gt; v[1]);
  return (
    <div>
      {Math.random()}
      <div>
        <span>Count: {count}</span>
        <button type="button"> dispatch({ type: 'increment' })}&gt;+1</button>
        <button type="button"> dispatch({ type: 'decrement' })}&gt;-1</button>
      </div>
    </div>
  );
};
github dai-shi / use-context-selector / examples / 02_typescript / src / Person.tsx View on Github external
const Person = () =&gt; {
  const person = useContextSelector(MyContext, (v) =&gt; v[0].person);
  const dispatch = useContextSelector(MyContext, (v) =&gt; v[1]);
  return (
    <div>
      {Math.random()}
      <div>
        First Name:
        <input value="{person.firstName}"> {
            const firstName = event.target.value;
            dispatch({ firstName, type: 'setFirstName' });
          }}
        /&gt;
      </div>
      <div>
        Last Name:</div></div>
github dai-shi / use-context-selector / examples / 01_minimal / src / index.js View on Github external
const Counter = () =&gt; {
  const count = useContextSelector(context, (v) =&gt; v[0].count);
  const dispatch = useContextSelector(context, (v) =&gt; v[1]);
  return (
    <div>
      {Math.random()}
      <div>
        <span>Count: {count}</span>
        <button type="button"> dispatch({ type: 'increment' })}&gt;+1</button>
        <button type="button"> dispatch({ type: 'decrement' })}&gt;-1</button>
      </div>
    </div>
  );
};
github dai-shi / use-context-selector / examples / 02_typescript / src / Person.tsx View on Github external
const Person = () =&gt; {
  const person = useContextSelector(MyContext, (v) =&gt; v[0].person);
  const dispatch = useContextSelector(MyContext, (v) =&gt; v[1]);
  return (
    <div>
      {Math.random()}
      <div>
        First Name:
        <input value="{person.firstName}"> {
            const firstName = event.target.value;
            dispatch({ firstName, type: 'setFirstName' });
          }}
        /&gt;
      </div>
      <div>
        Last Name:
        </div></div>
github dai-shi / will-this-react-global-state-work-in-concurrent-mode / src / use-context-selector / index.js View on Github external
import React, { useTransition } from 'react';
import { createContext, useContextSelector } from 'use-context-selector';

import {
  syncBlock,
  useRegisterIncrementDispatcher,
  initialState,
  reducer,
  ids,
  useCheckTearing,
  shallowEqual,
} from '../common';

const context = createContext(null);

const Provider = ({ children }) =&gt; {
  const [state, dispatch] = React.useReducer(reducer, initialState);
  return (
    
      {children}
    
  );
};

const Counter = React.memo(() =&gt; {
  const count = useContextSelector(context, (v) =&gt; v[0].count);
  syncBlock();
  return <div>{count}</div>;
}, shallowEqual);

use-context-selector

React useContextSelector hook in userland

MIT
Latest version published 7 months ago

Package Health Score

73 / 100
Full package analysis