2.0.23 • Published 24 days ago

widehook v2.0.23

Weekly downloads
-
License
MIT
Repository
github
Last release
24 days ago

Usage

Create hook

import { createWideHook } from 'widehook'

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

Use in component

const Button = () => {
  const [message, setMessage] = useMessage()

  return <button onClick={() => setMessage('One Value')}>
      {message}
  </button>
}

demo

Or even outside

const setSpecialMessage = (text: string) => {
  const [message, setMessage] = useMessage() // yes, it works here

  setMessage(text)
}

Options

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)
  },
})

returnObject: true

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

const useCounter = createWideHook({
  init: 3,
  returnObject: true,
  name: 'counter', // requires name
})

const { counter, setCounter } = useCounter() // in component

Types

Type declaration for init value:

type Text = 'One Text' | 'Another Text' | 'Completely Different Text'

export const useText = createWideHook({
  init: 'text' as Text,
})
2.0.23

24 days ago

2.0.15

4 months ago

2.0.16

4 months ago

2.0.13

4 months ago

2.0.14

4 months ago

2.0.11

4 months ago

2.0.12

4 months ago

2.0.19

4 months ago

2.0.17

4 months ago

2.0.18

4 months ago

2.0.22

4 months ago

2.0.20

4 months ago

2.0.21

4 months ago

2.0.5

4 months ago

2.0.4

4 months ago

2.0.7

4 months ago

2.0.6

4 months ago

2.0.9

4 months ago

2.0.8

4 months ago

2.0.10

4 months ago

2.0.3

5 months ago

2.0.2

5 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago

1.0.9

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

2.0.1

5 months ago

2.0.0

5 months ago

0.3.6

6 months ago

0.3.7

6 months ago

1.1.1

5 months ago

1.1.0

5 months ago

1.1.3

5 months ago

1.1.2

5 months ago

0.3.5

8 months ago

0.3.4

8 months ago

0.3.3

8 months ago

0.3.2

8 months ago

0.3.1

8 months ago

0.3.0

8 months ago

0.2.13

8 months ago

0.2.12

8 months ago

0.2.11

8 months ago

0.2.10

8 months ago

0.2.9

8 months ago

0.2.8

8 months ago

0.2.7

8 months ago

0.2.6

8 months ago

0.2.5

8 months ago

0.2.4

8 months ago

0.2.3

8 months ago

0.2.2

8 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.1.5

8 months ago

0.1.4

8 months ago

0.1.3

8 months ago

0.1.2

9 months ago

0.1.0

9 months ago

0.0.9

9 months ago

0.0.8

9 months ago

0.0.7

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago