React: Change order of arguments in useState

Created on 18 Dec 2018  路  5Comments  路  Source: facebook/react

In https://reactjs.org/docs/hooks-overview.html mentioned that useState(initialState) initialState is used only for initial render, and can be any type of data or nothing. In this case we can guess that initialSate is optional argument and as mostly used practice move it to the latest element of returned hook's pairs
let [stateValue, cb] = useState(initialState) to
let [cb, optionalStateValue] = useState(optionalInitState)

it should help us to avoid assign unused variables with goal to reach callback

Hooks

Most helpful comment

@eLeontev I think you can do const [ , setState] = useState(initState) if you want to ignore parts of the tuple.

All 5 comments

Why would you use useState if you aren't interested in the state value? the setter function isn't really useful apart from updating the state value. Are you trying to approximate a forceUpdate call?

@eLeontev I think you can do const [ , setState] = useState(initState) if you want to ignore parts of the tuple.

or hide it in a custom hook!

function useForceUpdate() {
  const [, forceUpdate] = useState()
  return forceUpdate
}

it looks pretty good, and that do you think about this approach?

let useStateReverse = (useState) => useState().reverse();
let [forseUpdate] = UseStateReverse(useState);

// or

let useStateCallback = (useState) => useState().pop();
let forseUpdate = useStateCallback(useState);

We don't plan to change this. Thanks for suggestion.

Was this page helpful?
0 / 5 - 0 ratings