0 votes
in React Hooks by
Can you initialise state from a function? Provide and example

1 Answer

0 votes
by
const StateFromFn = () => {

  const [token] = useState(() => {

    let token = window.localStorage.getItem("my-token");

    return token || "default#-token#"

  })

  return <div>Token is {token}</div>

}

Related questions

0 votes
asked Dec 17, 2023 in Google Cloud by AdilsonLima
0 votes
asked Nov 30, 2023 in Linux by JackTerrance
...