one-hook
Size
0.29 kb
View source

useGlobalState

Minimalistic & Performant global state manager.

Installation

npm install @1hook/use-global-state

Quick Start

Define the global state

use-count.ts
import {  } from '@1hook/use-global-state'

export const [] = ({ : 0 })

Use like useState

const [, ] = ()

Prevent unnecessary rerenders

To help prevent unnecessary rerenders, defineGlobalState provides a standalone setter function.
It helps prevent unnecessary rerenders by not listening to the state.

use-count.ts
export const [, ] = ({ : 0 })

✅ No rerender when count changes:

import {  } from './use-count'

function () {
  return (
    < ={() => (() =>  + 1)}>Increment</>
  )
}

❌ Rerenders when count changes:

import {  } from './use-count'

function () {
  const [, ] = ()

  return (
    < ={() => (() =>  + 1)}>Increment</>
  )
}

On this page