How to use use-subscription - 7 common examples

To help you get started, we’ve selected a few use-subscription 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 zeit / next.js / packages / next / next-server / lib / loadable.js View on Github external
const LoadableComponent = (props, ref) => {
    init()

    const context = React.useContext(LoadableContext)
    const state = useSubscription(subscription)

    React.useImperativeHandle(ref, () => ({
      retry: subscription.retry,
    }))

    if (context && Array.isArray(opts.modules)) {
      opts.modules.forEach(moduleName => {
        context(moduleName)
      })
    }

    if (state.loading || state.error) {
      return React.createElement(opts.loading, {
        isLoading: state.loading,
        pastDelay: state.pastDelay,
        timedOut: state.timedOut,
github Automattic / wp-calypso / packages / i18n-calypso / src / rtl.js View on Github external
function useRtl() {
		return useSubscription( RtlSubscription );
	}
github bvaughn / react-devtools-tutorial / src / hooks.js View on Github external
export function useLocation() {
  const subscription = useMemo(
    () => ({
      getCurrentValue: () => globalHistory.location,
      subscribe: callback => globalHistory.listen(callback),
    }),
    []
  );

  return useSubscription(subscription);
}
github dai-shi / will-this-react-global-state-work-in-concurrent-mode / src / mobx-use-sub / index.js View on Github external
function useCount(store) {
  return useSubscription(React.useMemo(() => ({
    getCurrentValue: () => store.count,
    subscribe: (cb) => mobx.autorun(cb),
  }), [store]));
}
github facebook / react-native / Libraries / Utilities / useColorScheme.js View on Github external
export default function useColorScheme(): ?ColorSchemeName {
  const subscription = useMemo(
    () => ({
      getCurrentValue: () => Appearance.getColorScheme(),
      subscribe: callback => {
        Appearance.addChangeListener(callback);
        return () => Appearance.removeChangeListener(callback);
      },
    }),
    [],
  );

  return useSubscription(subscription);
}
github dai-shi / will-this-react-global-state-work-in-concurrent-mode / src / use-subscription / index.js View on Github external
const Counter = React.memo(() => {
  const count = useSubscription(React.useMemo(() => ({
    getCurrentValue: () => store.getState().count,
    subscribe: (callback) => store.subscribe(callback),
  }), []));
  syncBlock();
  return <div>{count}</div>;
}, shallowEqual);
github ArnoSaine / react-pouchdb / src / utils / useSubscriptionImmediateSuspense.js View on Github external
export default function useSubscriptionImmediateSuspense(
  subscription,
  cleanupDelay = 30000
) {
  if (!subscription.getCurrentValue()) {
    throw new Promise(resolve => {
      const unsubscribe = subscription.subscribe(resolve);
      setTimeout(unsubscribe, cleanupDelay);
    });
  }
  return useSubscription(subscription);
}

use-subscription

Reusable hooks

MIT
Latest version published 16 days ago

Package Health Score

92 / 100
Full package analysis

Popular use-subscription functions