Q1. What are props in React?
Props (short for properties) are read-only inputs passed to components. They allow data to be passed from a parent component to a child component. Props are similar to function arguments and are accessible via the props object in functional components or this.props in class components.
Q2. How do you pass props to a component?
Props are passed as attributes in JSX. Example: . The child component receives them as an object: { name: 'Alice', age: 25 }. You can pass any JavaScript data type, including functions, arrays, and objects.
Q3. Are props mutable?
No, props are immutable. A component cannot modify its own props. If you need to change data, that data should be managed in the parent's state and passed down as props. The child can notify the parent via callback functions to request changes.
Q4. What is prop drilling?
Prop drilling refers to passing props through multiple levels of components even if intermediate components don't need them, just to reach a deeply nested child. This can make code harder to maintain. Solutions include using Context API or state management libraries like Redux.
Q5. How do you set default values for props?
You can define defaultProps on the component. For functional components: Component.defaultProps = { name: 'Guest' }. For class components, you can use static defaultProps = { name: 'Guest' }. Alternatively, you can use default parameters in functional components.
