Skip to content

useState

Александр edited this page Jan 2, 2024 · 2 revisions
export function useState<T>(initial: T): [getter: () => T, setter: (value: T) => void]

Accepts a initial state.

When value is changed, the component is updated.

import { afc, useState } from 'react-afc'

function Component(props) {
  const [getCount, setCount] = useState(0)

  function onCountInput(event) {
    setCount(+event.target.value)
  }

  function onButtonClick() {
    setCount(getCount() + 1)
  }

  return () => <>
    <p>Count: {getCount()}</p>
    <input value={getCount()} onChange={onCountInput}/>
    <button onClick={onButtonClick}>
      count++
    </button>
  </>
}

export default afc(Component)
Clone this wiki locally