Complete the code to create a React component that returns a simple heading.
function Welcome() {
return <h1>[1]</h1>;
}The component must return JSX with a string inside the <h1> tags. The string must be in quotes but without extra braces.
Complete the code to import React's useState hook for managing component state.
import React, [1] from 'react';
useState is the hook used to add state to functional components in React.
Fix the error in the code to correctly update state on button click.
const [count, setCount] = useState(0); function increment() { setCount([1]); }
State updates must use the new value directly. Using count + 1 returns the incremented value correctly.
Fill both blanks to create a simple SPA navigation using React Router.
import { BrowserRouter as [1], Route, Routes } from 'react-router-dom'; function App() { return ( <[2]> <Routes> <Route path="/" element={<Home />} /> </Routes> </[2]> ); }
BrowserRouter is the component that enables SPA navigation in React Router v6.
Fill all three blanks to create a React component that fetches data on mount and shows loading state.
import { useState, [1] } from 'react'; function DataFetcher() { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); [2](() => { fetch('/api/data') .then(res => res.json()) .then(json => { setData(json); setLoading(false); }); }, [3]); if (loading) return <p>Loading...</p>; return <pre>{JSON.stringify(data, null, 2)}</pre>; }
useEffect runs code on mount. The empty array [] as dependency means it runs only once.