Complete the code to create a React component that displays a greeting.
function Greeting() {
return <h1>{ [1] }</h1>;
}The text inside JSX must be a string wrapped in quotes.
Complete the code to use React's useState hook to create a counter.
import React, { [1] } from 'react';
useState is the hook to add state in functional components.
Fix the error in the React component that updates state on button click.
function Counter() {
const [count, setCount] = React.useState(0);
return (
<button onClick=[1]>
Count: {count}
</button>
);
}The onClick handler must be a function. Using an arrow function calls setCount correctly on click.
Fill both blanks to create a controlled input component in React.
function NameInput() {
const [name, setName] = React.useState('');
return (
<input
value=[1]
onChange={e => [2](e.target.value)}
aria-label="Name input"
/>
);
}The input's value is the state variable 'name'. The onChange calls 'setName' to update it.
Fill all three blanks to create a React component that fetches data on mount using useEffect.
import React, { [1], [2] } from 'react'; function DataFetcher() { const [data, setData] = [3](null); [1](() => { fetch('https://api.example.com/data') .then(response => response.json()) .then(json => setData(json)); }, []); if (!data) return <p>Loading...</p>; return <pre>{JSON.stringify(data, null, 2)}</pre>; }
useEffect runs code on mount. useState creates state. The component uses both to fetch and store data.