0
0
Reactframework~10 mins

Avoiding prop drilling in React - Interactive Code Practice

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

Complete the code to create a React context.

React
const ThemeContext = React.[1]();
Drag options to blanks, or click blank then click option'
AuseEffect
BcreateContext
CuseState
DuseContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using useState instead of createContext
Using useContext to create context (it's for consuming context)
2fill in blank
medium

Complete the code to provide context value to child components.

React
<ThemeContext.Provider value=[1]>
  {children}
</ThemeContext.Provider>
Drag options to blanks, or click blank then click option'
Atheme
Bcontext
Cstate
Dprops
Attempts:
3 left
💡 Hint
Common Mistakes
Passing props object instead of the specific value
Passing undefined or wrong variable
3fill in blank
hard

Fix the error in consuming context inside a functional component.

React
const theme = React.[1](ThemeContext);
Drag options to blanks, or click blank then click option'
AcreateContext
BuseState
CuseContext
DuseEffect
Attempts:
3 left
💡 Hint
Common Mistakes
Using createContext to consume context
Using useState or useEffect instead of useContext
4fill in blank
hard

Fill both blanks to create a context and provide a default value.

React
const UserContext = React.[1]([2]);
Drag options to blanks, or click blank then click option'
AcreateContext
BuseContext
C{}
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using useContext instead of createContext
Passing null when an object is expected
5fill in blank
hard

Fill all three blanks to consume context and display a user name.

React
const user = React.[1](UserContext);

// Assume user object has a 'name' property
// Use optional chaining to avoid errors
const displayName = user?.[3] || 'Guest';
return <p>Hello, [2]. Welcome back!</p>;
Drag options to blanks, or click blank then click option'
AuseContext
Buser.name
Cname
DcreateContext
EdisplayName
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access user.name directly without optional chaining
Using createContext instead of useContext to consume