Login
Remember
Register
Ask a Question
Can you initialise state from a function? Provide and example
0
votes
asked
May 30, 2023
in
React Hooks
by
Robindeniel
Can you initialise state from a function? Provide and example
initialisereactfunction
Please
log in
or
register
to answer this question.
1
Answer
0
votes
answered
May 30, 2023
by
Robindeniel
const StateFromFn = () => {
const [token] = useState(() => {
let token = window.localStorage.getItem("my-token");
return token || "default#-token#"
})
return <div>Token is {token}</div>
}
...