Skip to content

useObjectState

Александр edited this page Jan 17, 2023 · 1 revision
export function useObjectState<S>(initial: S): ObjectState<S>

Accepts a state object.

Returns the object { state, set<Key> }.

Has a superficial comparison of objects.

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

function Component(props) {
  const { state, setName, setAge } = useObjectState({
    name: 'Boris',
    age: 30
  })

  function changeAge() {
    setAge(20)
  }

  return () => <>
    <p>Name: {state.name}</p>
    <p>Age: {state.age}</p>
    <button onClick={changeAge}>
      Change age
    </button>
  </>
}

export default afc(Component)
Clone this wiki locally