react-state Flashcards

1
Q

What are hooks in React?

A

In React, useState, as well as any other function starting with ”use”, is called a Hook.

Hooks are special functions that are only available while React is rendering (which we’ll get into in more detail on the next page). They let you “hook into” different React features.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the “Rules of Hooks”? (if necessary, re-read the “Pitfall” box in State)

A

Hooks—functions starting with use—can only be called at the top level of your components or your own Hooks. You can’t call Hooks inside conditions, loops, or other nested functions. Hooks are functions, but it’s helpful to think of them as unconditional declarations about your component’s needs. You “use” React features at the top of your component similar to how you “import” modules at the top of your file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of state in React?

A

When you call useState, you are telling React that you want this component to remember something:

The state object is where you store property values that belongs to the component

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why can’t we just maintain state in a local variable?

A

Local variables don’t persist between renders. When React renders this component a second time, it renders it from scratch—it doesn’t consider any changes to the local variables.
Changes to local variables won’t trigger renders. React doesn’t realize it needs to render the component again with the new data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What two actions happen when you call a state setter function?

A

The state setter function (setIndex) which can update the state variable and trigger React to render the component again.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly