0
0
Reactframework~10 mins

React ecosystem overview - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the React library in a functional component.

React
import [1] from 'react';

function MyComponent() {
  return <div>Hello React!</div>;
}
Drag options to blanks, or click blank then click option'
AComponent
BReact
CuseState
Drender
Attempts:
3 left
💡 Hint
Common Mistakes
Using named imports like 'Component' instead of default import.
Forgetting to import React at all.
2fill in blank
medium

Complete the code to declare a state variable using React hooks.

React
import React, { [1] } from 'react';

function Counter() {
  const [count, setCount] = [1](0);
  return <button>{count}</button>;
}
Drag options to blanks, or click blank then click option'
AuseState
BuseEffect
CuseContext
DuseRef
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'useEffect' instead of 'useState'.
Not destructuring the array returned by the hook.
3fill in blank
hard

Fix the error in the code to correctly use the useEffect hook for side effects.

React
import React, { useEffect } from 'react';

function Logger() {
  useEffect(() => {
    console.log('Component mounted');
  }, [1]);
  return <div>Check console</div>;
}
Drag options to blanks, or click blank then click option'
A{}
Bnull
C[]
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an object or null instead of an array.
Omitting the second argument causing effect to run on every render.
4fill in blank
hard

Fill both blanks to create a context and provide it to child components.

React
import React, { createContext, [1] } from 'react';

const ThemeContext = createContext('light');

function App() {
  return (
    <ThemeContext.Provider value=[2]>
      <Child />
    </ThemeContext.Provider>
  );
}
Drag options to blanks, or click blank then click option'
AuseContext
B'dark'
C'light'
DuseState
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'useState' instead of 'useContext'.
Providing the wrong value string to the provider.
5fill in blank
hard

Fill all three blanks to create a memoized callback that updates state.

React
import React, { useState, [1], useCallback } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  const increment = useCallback(() => {
    setCount(count [2] 1);
  }, [[3]]);
  return <button onClick={increment}>{count}</button>;
}
Drag options to blanks, or click blank then click option'
AuseEffect
B+
Ccount
DuseMemo
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+'.
Leaving dependency array empty causing stale closures.
Importing wrong hooks.