Choose the option that best describes the key difference between state and props in React components.
Think about who controls the data and whether it can be changed inside the component.
State is data that a component controls and can change over time. Props are data passed down from a parent component and cannot be changed by the receiving component.
Given the component below, what will be shown on the screen?
import React, { useState } from 'react'; function Greeting(props) { const [name, setName] = useState('Alice'); return <h1>Hello, {props.name} and {name}!</h1>; } // Usage: <Greeting name="Bob" />
Look at how props and state are used inside the component.
The component receives name as a prop with value "Bob" and has internal state name initialized to "Alice". It displays both values.
Which of the following statements correctly describes when a React functional component re-renders?
Think about what triggers React to update the UI.
React re-renders a component whenever its state or props change, so the UI stays up to date.
Choose the option that correctly updates the count state variable by adding 1.
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); function increment() { // Update count here } return <button onClick={increment}>Count: {count}</button>; }
Consider the safest way to update state when relying on previous value.
Using the updater function setCount(prev => prev + 1) ensures the latest state is used, avoiding bugs in asynchronous updates.
Examine the code below. The count does not change on button clicks. What is the cause?
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); function increment() { count = count + 1; } return <button onClick={increment}>Count: {count}</button>; }
Think about how React detects changes to update the UI.
State variables in React must be updated using their setter functions (like setCount) to trigger re-rendering. Direct assignment does not work.