React.js Questions Flashcards
(46 cards)
What is useState?
useState is a React Hook that allows you to add state to functional components.
What does useState return?
An array where the first element is the current state value and the second element is a function to update that state.
What happens when setCount(newCount) is called?
The component will re-render with the new state value.
What occurs when a component receives new props?
The component will re-render.
What is React’s reconciliation process?
React compares new props to previous ones, and if there’s a difference, the component is updated with the new data.
How can you share state between multiple components?
By lifting the state up to a common parent component or using Context API or a state management library like Redux.
Is JSX mandatory to use React?
No, JSX is not mandatory.
How can you define components without JSX?
By writing JavaScript that uses React.createElement().
What is a controlled component?
A component where form data is handled by React state.
What is an uncontrolled component?
A component that manages its own state internally, and React doesn’t control the form data.
What is the Virtual DOM?
An in-memory representation of the actual DOM.
How does the Virtual DOM improve performance in React?
It allows React to update the UI efficiently by comparing the new VDOM with the previous one.
What are some common pitfalls when doing data fetching?
- Not handling errors properly
- Forgetting to cancel requests when a component unmounts
- Not managing loading states
- Making repeated requests unnecessarily
- Incorrectly handling asynchronous data
What is useEffect used for?
To perform side effects in a functional component.
What are some pitfalls of useEffect?
- Forgetting to add dependencies
- Using it for synchronous operations
- Overcomplicating logic within one effect
What are the advantages of using React?
- Component-based architecture
- Virtual DOM for improved performance
- Large ecosystem of tools and libraries
- Backed by Facebook
What is the difference between functional components and class components?
Functional components are simpler functions, while class components are ES6 classes that extend React.Component.
How does React’s reconciliation process work?
React compares the current Virtual DOM with the previous one to identify changed elements and applies minimal changes to the real DOM.
What are hooks in React?
Functions that allow you to hook into React’s state and lifecycle features from functional components.
What is useReducer and when would you use it?
useReducer is a hook for managing complex state logic and is useful for multiple interdependent state variables.
How do you manage form state in React?
Using controlled components where input values are tied to the component’s state or using uncontrolled components with refs.
What is the purpose of keys in React lists?
To help React identify which items in a list have changed, added, or removed.
How do you pass data from a child to a parent component in React?
By passing a callback function as a prop to the child, which the child calls to send data back.
What is React.memo?
A higher-order component that prevents unnecessary re-renders by memoizing the component output based on props.