Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a functional React component named Greeting.
React
function Greeting() {
return <h1>[1];</h1>;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving text unquoted inside JSX
Using angle brackets around text
✗ Incorrect
The component must return JSX with a string inside quotes to render text correctly.
2fill in blank
mediumComplete the code to import React's useState hook.
React
import React, [1] from 'react';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing useEffect instead of useState
Trying to import Component in functional components
✗ Incorrect
useState is the hook used to add state to functional components.
3fill in blank
hardFix the error in the component by completing the return statement correctly.
React
function Welcome() {
return [1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Missing opening angle bracket
Returning plain string without JSX tags
✗ Incorrect
The return must be a valid JSX element wrapped in angle brackets and properly closed.
4fill in blank
hardFill both blanks to create a component that uses useState to toggle text.
React
import React, { [1] } from 'react'; function Toggle() { const [isOn, setIsOn] = [2](false); return ( <button onClick={() => setIsOn(!isOn)}> {isOn ? 'ON' : 'OFF'} </button> ); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing useEffect but calling useState
Using useReducer instead of useState
✗ Incorrect
useState is imported and called to create state and its setter function.
5fill in blank
hardFill all three blanks to create a component that fetches data on mount using useEffect.
React
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)); }, []); return <pre>{JSON.stringify(data, null, 2)}</pre>; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using useContext instead of useState for data
Not importing useEffect
✗ Incorrect
useEffect is imported and used to run code on mount; useState is imported and used to hold data state.