Home
Recent Q&A
Java
Cloud
JavaScript
Python
SQL
PHP
HTML
C++
Data Science
DBMS
Devops
Hadoop
Machine Learning
Azure
Blockchain
Devops
Ask a Question
Can you initialise state from a function? Provide and example
Home
React Hooks
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>
}
Related questions
0
votes
Q: Provide an example of any simple Custom React Hook. Why do we need Custom Hooks?
asked
May 30, 2023
in
React Hooks
by
Robindeniel
customhooks
0
votes
Q: How can you access the state of a component from inside of a member function?
asked
May 20, 2020
in
ReactJS
by
SakshiSharma
#reactjs-member-function
#reactjs-function
0
votes
Q: Do two components using the same Hook share state?
asked
May 30, 2023
in
React Hooks
by
Robindeniel
reacthook
0
votes
Q: What are the different types of loops in Bash and can you provide a brief example of each?
asked
Feb 7
in
JavaScript
by
john ganales
bash-script
0
votes
Q: Can you provide an example of when you would use a transaction in Firestore and why it’s necessary?
asked
Dec 17, 2023
in
Google Cloud
by
AdilsonLima
google-cloud-interview-questions-answers
0
votes
Q: Can you explain the concept of a BroadcastReceiver in Android and provide an example of when you would use it?
asked
Aug 30, 2023
in
Android Library
by
SakshiSharma
broadcastreceiver
0
votes
Q: Can you provide an example of how to implement a secure HTTP interceptor
asked
Feb 23
in
Angular
by
rahuljain1
httpinterceptor
0
votes
Q: Can you provide an example of using Firestore’s onSnapshot listener in a real-world application?
asked
Dec 19, 2023
in
Google Cloud
by
GeorgeBell
google-cloud-interview-questions-answers
0
votes
Q: Can you provide an example of how a Segmentation Fault might occur in C or C++?
asked
Nov 28, 2023
in
C Plus Plus
by
JackTerrance
segmentation-fault-interview-questions
0
votes
Q: Can you explain how Zsh’s associative arrays work? Provide an example use case.
asked
Nov 30, 2023
in
Linux
by
JackTerrance
zsh-interview-questions
...