Skip to content
alevnyacow edited this page Mar 24, 2022 · 1 revision

1. Methods

1-1. createUseSharedVariable

Description

Generates a tuple of two hooks:

  • first hook is a React variable generator
  • second hook can be used to rewrite a whole variable (you don't need this most likely)

Signature

function createUseSharedVariable<T extends object>(initialState: T): 
    readonly [ReactVariableHook<T>, ReactVariableRewriteHook<T>]

Take a look at the Types section to get description of ReactVariableHook and ReactVariableRewriteHook types.

Example

const [useTimer, useWholeTimerRewrite] = createUseSharedVariable({
    ticks: 0
});

2. Types

2-1. ReactVariableHook

Description

Describes a hook which returns a shared React variable. It's a generic type taking a type of an output React variable. This hook takes one parameter (optional rerenderOnChange boolean flag, set to true by default) can be used to prevent a rerender on React variable changes.

Signature

type ReactVariableHook<T> = (rerenderOnChange?: boolean) => T;

2-2. ReactVariableRewriteHook

Description

Describes a hook can be used to rewrite a whole React variable. It needs a generator to work. The generator is a function which takes previous variable state and returns target variable state. Pretty like a setState function, yeah.

Signature

type ReactVariableRewriteHook<T> = () => (
    generator: (oldState: T) => T
) => void;
Clone this wiki locally