Giter VIP home page Giter VIP logo

re-use's Introduction

@lxsmnsyc/re-use

⚛️ 🎣 A collection of hooks for ReasonReact

NPM JavaScript Style Guide

Install

npm install --save @lxsmnsyc/re-use
yarn add @lxsmnsyc/re-use

bsconfig.json

  "bs-dependencies": [
    "@lxsmnsyc/re-use"
  ],

Hooks

  • Constant
  • ConstantCallback
  • ForceUpdate
  • IsomorphicEffect
  • LifeCycle
  • MountedState
  • Mutable
  • NativeCallback
  • NativeEffect
  • NativeIsomorphicEffect
  • NativeLayoutEffect
  • NativeMemo
  • NativeState
  • NativeSyncEffect
  • OnMount
  • OnUnmount
  • PageVisibility
  • Previous
  • Promise
  • RefMounted
  • SyncEffect
  • ThrottledReducer
  • ThrottledState
  • UpdateEffect
  • WindowSize

License

MIT © lxsmnsyc

re-use's People

Contributors

lxsmnsyc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

idkjs

re-use's Issues

[Feature] useMountedState

Wrapper for useRefMounted which returns a function instead of a mutable reference, preventing users on overriding the reference's value.

let call: unit => (unit => bool);

add useConstantCallback

since useCallback works like useMemo, leverage useConstant to produce constant callbacks.

let call: ('input => 'output) => ('input => 'output);

[Feature] useWindowSize

useWindowSize provides the window's current width and height and reacts to its value changes.

useWindowSize returns a record with fields width and height.

type t = {
  width: int,
  height: int,
};

let call: unit => t;

add useEffectOnce

an effect that is only executed on once.

let call: (unit => option(unit => unit)) => unit;

[Feature] useThrottledCallback

Hook for wrapping a given callback into a throttled callback.

let call: ('input => 'output) => int => ('input => 'output);
let call1: ('input => 'output) => array('a) => ('input => 'output);
let call2: (('input => 'output), int, ('a, 'b)) => ('input => 'output);
let call3: (('input => 'output), int, ('a, 'b, 'c)) => ('input => 'output);
let call4: (('input => 'output), int, ('a, 'b, 'c, 'd)) => ('input => 'output);
let call5: (('input => 'output), int, ('a, 'b, 'c, 'd, 'e)) => ('input => 'output);
let call6: (('input => 'output), int, ('a, 'b, 'c, 'd, 'e, 'f)) => ('input => 'output);
let call7: (('input => 'output), int, ('a, 'b, 'c, 'd, 'e, 'f, 'g)) => ('input => 'output);

[Feature] usePromise

Creates a promise wrapper function which resolves only if the component is still mounted (depending on the given dependencies).

let call: unit => (Js.Promise.t('a) => Js.Promise.t('a));
let call0: unit => (Js.Promise.t('a) => Js.Promise.t('a));
let call1: array('a) => (Js.Promise.t('r) => Js.Promise.t('r));
let call2: ('a, 'b) => (Js.Promise.t('r) => Js.Promise.t('r));
let call3: ('a, 'b, 'c) => (Js.Promise.t('r) => Js.Promise.t('r));
let call4: ('a, 'b, 'c, 'd) => (Js.Promise.t('r) => Js.Promise.t('r));
let call5: ('a, 'b, 'c, 'd, 'e) => (Js.Promise.t('r) => Js.Promise.t('r));
let call6: ('a, 'b, 'c, 'd, 'e, 'f) => (Js.Promise.t('r) => Js.Promise.t('r));
let call7: ('a, 'b, 'c, 'd, 'e, 'f, 'g) => (Js.Promise.t('r) => Js.Promise.t('r));

[Bug] useIsomorphicEffect should be constant.

useIsomorphicEffect is currently written to check if window is undefined everytime it is called. It will be buggy if window value changed dynamically, altering the used hook for useIsomorphicEffect, and could lead into unexpected behavior such as disparity between state indeces for the hooks, cleanup function not being called, etc.

[Feature] useNativeLayoutEffect

A variation of useLayoutEffect which accepts a single value as a dependency and performs the built-in equality comparison by ReasonML.

let call: (unit => option(unit => unit)) => 'a => unit;

add useIsomorphicEffect

SSR does not support useLayoutEffect (as the DOM does not exist yet when the page is being delivered.). useIsomorphicLayout solves the issue by providing useLayoutEffect whenever the DOM becomes available, fallbacks to useEffect if it isn't.

let call: (unit => option(unit => unit)) => unit;
let call1: (unit => option(unit => unit)) => array('a) => unit;
let call2: (unit => option(unit => unit)) => ('a, 'b) => unit;
let call3: (unit => option(unit => unit)) => ('a, 'b, 'c) => unit;
let call4: (unit => option(unit => unit)) => ('a, 'b, 'c, 'd) => unit;
let call5: (unit => option(unit => unit)) => ('a, 'b, 'c, 'd, 'e) => unit;
let call6: (unit => option(unit => unit)) => ('a, 'b, 'c, 'd, 'e, 'f) => unit;
let call7: (unit => option(unit => unit)) => ('a, 'b, 'c, 'd, 'e, 'f, 'g) => unit;

Usage:

ReUse.IsomorphicEffect.call(() => {
  // some logic 
});

[Feature] useNativeEffect

A variation of useEffect which accepts a single value as a dependency and performs the built-in equality comparison by ReasonML.

let call: (unit => option(unit => unit)) => 'a => unit;

[Feature] useDebouncedReducer

A hook for producing a debounced useReducer.

let call: ('state => 'action => 'state) => int => 'state => ('state, 'action => unit);

add useForceUpdate

A hook that returns a function that re-renders the component when called.

let call: unit => unit => unit;

Usage

let forceUpdate = ReUse.ForceUpdate.call();

forceUpdate(); // re-renders the component.

[Feature] useNativeMemo

A variant of the useMemo that accepts a single dependency value and is compared through ReasonML's equality comparison.

let call: (unit => 'a) => 'b => 'a;

add useUpdateEffect

an effect that only executes if the component updates. Leverages IsomorphicEffect.

let call: (unit => option(unit => unit)) => unit;
let call1: (unit => option(unit => unit)) => array('a) => unit;
let call2: (unit => option(unit => unit)) => ('a, 'b) => unit;
let call3: (unit => option(unit => unit)) => ('a, 'b, 'c) => unit;
let call4: (unit => option(unit => unit)) => ('a, 'b, 'c, 'd) => unit;
let call5: (unit => option(unit => unit)) => ('a, 'b, 'c, 'd, 'e) => unit;
let call6: (unit => option(unit => unit)) => ('a, 'b, 'c, 'd, 'e, 'f) => unit;
let call7: (unit => option(unit => unit)) => ('a, 'b, 'c, 'd, 'e, 'f, 'g) => unit;

[Feature] useNativeIsomorphicEffect

A variation of useEffect which accepts a single value as a dependency and performs the built-in equality comparison by ReasonML.

let call: (unit => option(unit => unit)) => 'a => unit;

[Feature] useDebouncedCallback

Hook for wrapping a given callback into a debounced callback.

let call: ('input => 'output) => int => ('input => 'output);
let call1: ('input => 'output) => array('a) => ('input => 'output);
let call2: (('input => 'output), int, ('a, 'b)) => ('input => 'output);
let call3: (('input => 'output), int, ('a, 'b, 'c)) => ('input => 'output);
let call4: (('input => 'output), int, ('a, 'b, 'c, 'd)) => ('input => 'output);
let call5: (('input => 'output), int, ('a, 'b, 'c, 'd, 'e)) => ('input => 'output);
let call6: (('input => 'output), int, ('a, 'b, 'c, 'd, 'e, 'f)) => ('input => 'output);
let call7: (('input => 'output), int, ('a, 'b, 'c, 'd, 'e, 'f, 'g)) => ('input => 'output);

[Feature] useThrottledReducer

A hook for producing a throttled useReducer.

let call: ('state => 'action => 'state) => int => 'state => ('state, 'action => unit);

[Feature] useNativeState

a variation of useState but instead of using the default Object.is function to bail out of re-render, it uses the ReasonML's equality comparison.

let call: (unit => 'a) => ('a, ('a => 'a) => unit);

[Feature] usePageVisibility

usePageVisibility uses the document.visibilityState property and visibilitychange event to keep track of the page visibility's state. This is useful for pausing background processes like animation when the page goes hidden.

This hook provides a boolean value that represents the page visibility state: true if it is visibile, false otherwise.

// PageVisibility
let call: unit => bool;

[Feature] useNativeSyncEffect

A variation of useSyncEffect that receives a single dependency value and is compared using the ReasonML's equality comparison

let call: (unit => option(unit => unit)) => 'a => unit;

add usePrevious

a hook that holds the state of a value before re-renders.

let call: 'a => option('a);

add useRefMounted

leverages useMutable to track lifecycle changes.

let call: unit => ref(bool);

[Feature] useThrottledState

A hook that allows to throttle setState with a given timeout. Same syntax as a useState but accepts a timeout first.

let call: int => 'a => ('a, 'a => 'a => unit);

[Feature] useNativeCallback

A variant of the useCallback that accepts a single dependency value and is compared through ReasonML's equality comparison.

let call: ('input => 'output) => 'a => ('input => 'output);

[Feature] useSyncEffect

Runs a blocking side-effect synchronously on render.

let call: (unit => option(unit => unit)) => unit;
let call0: (unit => option(unit => unit)) => unit;
let call1: (unit => option(unit => unit)) => array('a) => unit;
let call2: ((unit => option(unit => unit)), ('a, 'b)) => unit;
let call3: ((unit => option(unit => unit)), ('a, 'b, 'c)) => unit;
let call4: ((unit => option(unit => unit)), ('a, 'b, 'c, 'd)) => unit;
let call5: ((unit => option(unit => unit)), ('a, 'b, 'c, 'd, 'e)) => unit;
let call6: ((unit => option(unit => unit)), ('a, 'b, 'c, 'd, 'e, 'f)) => unit;
let call7: ((unit => option(unit => unit)), ('a, 'b, 'c, 'd, 'e, 'f, 'g)) => unit;

add useMutable

Leverage the mutable/ref('a) feature of Reason, an optional replacement for React's useRef (but not for passing it to ref property).

// Mutable
let call: 'a => ref('a);

Usage:

let myRef = ReUse.Mutable.call(0);

Js.log(myRef^); // 0

[Feature] useDebouncedState

A hook that allows to debounce setState with a given timeout. Same syntax as a useState but accepts a timeout first.

let call: int => 'a => ('a, 'a => 'a => unit);

WindowSize component has typing error

Describe the bug
The WindowSize component hat an error.
addEventListener(window, "resive", handler);
Is required to change to:
addEventListener(window, "resize", handler);

add useLayoutEffectOnce and useIsomorphicEffectOnce

variants of useEffectOnce, can also be used for lifecycle hooks.

// LayoutEffectOnce
let call: (unit => option(unit => unit)) => unit;

// IsomorphicEffectOnce
let call: (unit => option(unit => unit)) => unit;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.