Props and State

Props and State

This section introduces the foundational concepts of React, focusing on JSX and component creation. Understanding these basics is crucial for building robust React applications, as they set the stage for more advanced topics.

10 audio · 1:56

Nortren·

What are props in React?

0:11
Props, short for properties, are read-only inputs passed from a parent component to a child component. They allow components to be configured and reused, and a child component must never modify its own props.

What is state in React?

0:11
State is data managed inside a component that can change over time, usually in response to user actions or network responses. When state changes, React re-renders the component to reflect the new data on the screen.

What is the difference between props and state?

0:12
Props are passed into a component from its parent and are read-only inside that component. State is owned and managed inside the component itself and can be updated with a state setter from useState or with this.setState in a class.

What is lifting state up in React?

0:11
Lifting state up means moving state from a child component to the closest common ancestor of components that need to share it. The ancestor passes the state and setters down as props, keeping the shared data in one place.

What is prop drilling?

0:12
Prop drilling is the practice of passing props through many intermediate components that do not use them, just to reach a deeply nested child. It can be reduced by using React Context, composition, or a state management library.

Why should setState calls be treated as asynchronous?

0:12
State setters in React do not update the state immediately; they schedule an update. Reading state right after calling the setter returns the old value. If the next state depends on the previous one, you should pass a function to the setter.

What is the functional form of useState's setter?

0:13
The setter returned by useState can accept a function instead of a value. The function receives the previous state and returns the next state. This form is safer when updates depend on the current state, especially with batching and concurrent updates.

What is state colocation?

0:10
State colocation is the practice of keeping state as close as possible to the components that actually use it. It is React's default recommendation and avoids unnecessary re-renders in unrelated parts of the tree.

What is unidirectional data flow in React?

0:14
Unidirectional data flow means data moves in a single direction: from parent components down to children through props. Children communicate upward by calling callback functions passed as props. This makes data changes predictable and easier to debug.

What is the purpose of defaultProps?

0:10
defaultProps lets you define default values for props that are not passed by the parent. In modern functional components, default parameter values in the function signature are typically used instead. ---