widehook
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

widehook.js

widehook logo

Create hook

import { createWideHook } from 'widehook'

export const useMessage = createWideHook({
  init: 'text',
})

Use inside component

const Button = () => {
  const [message, setMessage] = useMessage()
  
  const click = () => setMessage('One Value')

  return <button onClick={click}>
      {message}
  </button>
}

Or even outside

const setSpecialMessage = (text: string) => {
  const [message, setMessage] = useMessage() 

  setMessage(text)
}

Hook options

objectifyWithName: "stateName"

If set - hook returns an object with named props and methods:

const useCounter = createWideHook({
  init: 3,
  objectifyWithName: 'counter', 
})

...

const { counter, setCounter } = useCounter()

on(state, setState) { }

On each "setState" define action:

export const useMessage = createWideHook({
  init: 'text',
  on(text, setText) {
    if (text === 'specific message') setText('another text')
  },
})

Access another state

Take another widehook to read and update:

export const useText = createWideHook({
  init: 'text',
  on(text, setText) {
    const [number, setNumber] = useNumber()
    if (text === 'specific text') setNumber(7)
  },
})

/widehook/

    Package Sidebar

    Install

    npm i widehook

    Weekly Downloads

    8

    Version

    3.0.1

    License

    MIT

    Unpacked Size

    37.2 kB

    Total Files

    13

    Last publish

    Collaborators

    • yorkblansh