Questions
What is the primary purpose of using components in React?
- To reuse code and enhance modularity
- To improve performance and reduce bundle size
- To handle state management and data flow
- To create custom UI elements and interactions
What are props in React?
- Properties passed from parent components to child components
- Methods used to handle user interactions and events
- State variables that store data within a component
- Hooks that allow you to access React's internal features
How do you define props in a React component?
- Using the
propskeyword in the constructor method - As arguments to the component function
- Inside the
render()method of the component - Through the use of
useState()anduseEffect()hooks
What is the difference between props and state in React?
-
propsare immutable, whilestatecan be modified -
propsare passed from parent to child, whilestateis local to a component - Both
propsandstatecan be used to store data - All of the above
How do you access props within a React component?
- Using the
this.propsobject - Through the
propsargument passed to the component function - Inside the
render()method usingprops - Both A and B
Which lifecycle method is called when a React component is first mounted?
-
componentDidMount() -
componentWillMount() -
render() -
constructor()
What is the purpose of the shouldComponentUpdate() lifecycle method?
- To determine if a component should update when its props or state change
- To optimize performance by preventing unnecessary re-renders
- To handle state updates and data manipulation
- To communicate with other components in the application
Which React hook is used to manage state within a functional component?
-
useState() -
useEffect() -
useContext() -
useReducer()
What is the purpose of the useEffect() hook in React?
- To perform side effects such as fetching data or setting up event listeners
- To manage state within a functional component
- To optimize performance by preventing unnecessary re-renders
- To communicate with other components in the application
What is the difference between a controlled component and an uncontrolled component in React?
- Controlled components are managed by the state of the parent component, while uncontrolled components are managed by their own state
- Controlled components use the
valueprop, while uncontrolled components use thedefaultValueprop - Controlled components allow for more fine-grained control over the input value, while uncontrolled components are simpler to implement
- All of the above
How do you pass data from a child component to a parent component in React?
- Using props
- Using the
this.propsobject - Using the
stateobject - Using event handlers
What is the purpose of the context API in React?
- To provide a way to share data between components without passing props
- To manage state within a functional component
- To optimize performance by preventing unnecessary re-renders
- To communicate with other components in the application
Which React hook is used to access the context API?
-
useContext() -
useEffect() -
useState() -
useReducer()
What is the purpose of the forwardRef() API in React?
- To pass a ref from a parent component to a child component
- To manage state within a functional component
- To optimize performance by preventing unnecessary re-renders
- To communicate with other components in the application
What is the difference between a static and a dynamic prop in React?
- Static props are defined at compile-time, while dynamic props are defined at runtime
- Static props can be modified, while dynamic props cannot
- Static props are passed from parent to child, while dynamic props are passed from child to parent
- None of the above