1. What is the usage of setState()?
setState() schedules an update to the component’s state and re-renders it. It can also accept a callback that runs after the update is complete.
Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
React · Question Set
Components, Props & State interview questions for placements and exams.
Questions
17
Included in this set
Subject
React
Explore more sets
Difficulty
Mixed
Level of this set
Go through each question and its explanation. Use this set as a focused practice pack for React.
setState() schedules an update to the component’s state and re-renders it. It can also accept a callback that runs after the update is complete.
Props are used to pass data from parent components to child components. They make components reusable and customizable.
React component lifecycle includes three phases: Mounting (component created and added to DOM), Updating (re-render due to state/prop changes), and Unmounting (component removed from DOM). Hooks like useEffect can handle these phases in functional components.
Refs let you directly access a DOM element. In uncontrolled components, you use refs to read form values without React managing the input state.
const inputRef = useRef();
const handleSubmit = () => {
alert(inputRef.current.value);
};Functional components are simple JavaScript functions that return JSX. Class components are ES6 classes that extend React.Component and use a render() method. Functional components are now preferred because they are simpler and support Hooks.
// Functional Component
function Welcome() {
return <h1>Hello!</h1>;
}
// Class Component
class Welcome extends React.Component {
render() {
return <h1>Hello!</h1>;
}
}Functional components are lightweight and rely on Hooks for state and lifecycle. Class components use the render() method, have access to lifecycle methods, and use this.state. Hooks made functional components capable of everything classes can do with less code.
In functional components, props are received as function arguments. In class components, they are accessed via this.props. Props are read-only in both cases.
// Functional
function Hello(props) { return <p>{props.name}</p>; }
// Class
class Hello extends React.Component { render() { return <p>{this.props.name}</p>; } }In class components, state is managed with this.state and updated using setState(). In functional components, state is handled with the useState() Hook. Both trigger re-renders when the state changes.
// Functional
const [count, setCount] = useState(0);
// Class
this.state = { count: 0 };
this.setState({ count: this.state.count + 1 });useState() is a Hook that allows you to add state to functional components. It returns an array with the current state and a function to update it. When you call the setter function, React re-renders the component.
const [name, setName] = useState('Guest');
setName('John'); // updates state and re-renders componentIn controlled components, form data is handled by React state. In uncontrolled components, the DOM itself manages the input value using refs. Controlled components provide better validation and consistency.
// Controlled
<input value={text} onChange={(e) => setText(e.target.value)} />
// Uncontrolled
<input ref={inputRef} />Controlled components make React the single source of truth for input values. This gives better control for validation, conditionally disabling inputs, and synchronizing with state.
setState() is used to update a class component’s state. It merges the new values with the existing state and triggers a re-render.
this.setState({ count: this.state.count + 1 });Directly changing state (like this.state.count = 1) won’t trigger a re-render and can cause bugs. Always use setState() or Hooks to update state safely.
Side effects are operations that affect something outside the component, like data fetching, subscriptions, or manually changing the DOM. These are handled using the useEffect Hook.
There are two types: effects without cleanup (like API calls or logging) and effects with cleanup (like removing event listeners or cancelling timers). useEffect can handle both types using return functions.
useEffect(() => {
const timer = setInterval(() => console.log('tick'), 1000);
return () => clearInterval(timer); // cleanup
}, []);React cleans up side effects automatically when a component unmounts or before the effect runs again. Returning a cleanup function inside useEffect prevents memory leaks.
State represents the internal data of a component that can change over time and cause re-renders when updated.
For complete preparation, combine this set with full subject-wise practice for React. You can also explore other subjects and sets from the links below.