Complete the code to create a new React context.
import React from 'react'; const MyContext = React.[1]();
The createContext function is used to create a new React context object.
Complete the code to provide the context value to child components.
function MyProvider({ children }) {
return (
<MyContext.Provider value={{ name: 'Alice' }}>
[1]
</MyContext.Provider>
);
}The children prop represents the nested components that will receive the context.
Fix the error in the code to consume the context value inside a component.
import React, { [1] } from 'react'; function MyComponent() { const context = [1](MyContext); return <div>{context.name}</div>; }
The useContext hook is used to read the current context value inside a component.
Fill both blanks to create and export a context with a default value.
import React from 'react'; const [1] = React.[2]({ name: 'Guest' }); export default [1];
You create a context named UserContext using createContext with a default value.
Fill all three blanks to consume context and display a greeting.
import React, { [1] } from 'react'; import UserContext from './UserContext'; function Greeting() { const user = [2](UserContext); return <h1>Hello, [3]!</h1>; }
Use useContext to get the user object, then display user.name in the greeting.
