DEV Community

Dillon
Dillon

Posted on

State

State - used for values that change over time.

useState creates state variables

useState takes a single value, which is the base state, initial value.

React.useState(0);

useState is a hook

useState returns an array with current value of state variable and a funciton you can use to update the astate variable

in the example of Jonny Appleseed's apple counter you would have the following syntax

function Appledieapp() {
const [apple, setApple] = React.useState(0);
return (
setApple(apple + 1)}>
Apples: {apple}

);
}

Top comments (2)

Collapse
 
dillpap profile image
Dillon

React forms are weird...

funciton ComponentForm () {
const [searchTerm, setSearchTerm] = React.useState('what would you like to search');
return (
<>

Search:

React.useState('what would you like to search');

to fix this have...

{
setSearchTerm(event.target.value)
}}
/>

Collapse
 
dillpap profile image
Dillon

When you are doing searches or forms, a value cannot go from undefined to suddenly defined.

It can go from empty string ('') to having a value, but it can't go from () to having a value.