DEV Community

Kelvin Acosta
Kelvin Acosta

Posted on

Learning React

It's been my first time learning about this technology. I will not lie about this. It has been super hard learning for me. One thing that was not getting into my mind its states.
"A state represents the current condition or state of the component, which can change over time due to user interaction, data updates, or other factors". Best definition I got it when I was doing my research.

When I got to this point I was confused but I get now:

const [recipe, setRecipe] = useState(null);

.Declaring a constant variable

.[recipe, setRecipe] uses array destructuring syntax to declare two variables.

.It assigns the first value returned by the useState hook to the recipe variable and the second value to the setRecipe variable.
.By using setRecipe, you can later update the recipe state by invoking it with a new value.
."null" In this case it is just the initial value as argument given.

This line of code initializes a state variable called recipe with an initial value of null and provides a function setRecipe that can be used to update the recipe state.

As a key to remember: " useState, the first element of the returned array (recipe in this case) represents the current value of the state variable, while the second element (setRecipe) is a function that allows you to update the state value"

Top comments (0)