In React, functional components can use state by using useState(). Let’s look at a code example:
const [mode, setMode] = React.useState(0);
In the above code block we are destructuring the return value of useState. mode is a read-only constant, that can be read and used. For changing the value the setMode can be used.
The usual naming convention is the constant name for the first object and setVariableName for the function, but the function name can be anything.
The following code example demonstrates accessing the variable and updating the variable.
{{mode}}
setMode(1);
But any attempts to modify the value of the constant would result in an error.
–
Mr. Kanti Kalyan Arumilli
B.Tech, M.B.A
Founder & CEO, Lead Full-Stack .Net developer
ALight Technology And Services Limited
Phone / SMS / WhatsApp on the following 3 numbers:
+91-789-362-6688, +1-480-347-6849, +44-07718-273-964
+44-33-3303-1284 (Preferred number if calling from U.K, No WhatsApp)
kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.