📖 Scenario: You are building a simple website using Astro. You want to share user information like their name and role across different components without passing props manually each time.
🎯 Goal: Create a user context in Astro and use it inside two components to display the user's name and role.
📋 What You'll Learn
Create a user object with name and role
Create a context key for the user
Provide the user context in a parent component
Consume the user context in two child components
💡 Why This Matters
🌍 Real World
Sharing user information like login status or preferences across multiple components in a website without passing props repeatedly.
💼 Career
Understanding context is key for building scalable and maintainable component-based web applications, a common requirement in frontend development jobs.
Progress0 / 4 steps
1
Create the user data object
Create a constant called user with an object containing name set to "Alice" and role set to "admin".
Astro
Hint
Use const user = { name: "Alice", role: "admin" }; to create the user object.
2
Create a user context key
Create a constant called UserContext and assign it the result of Astro.createContext() to define a new context key.
Astro
Hint
Use const UserContext = Astro.createContext(); to create the context key.
3
Provide the user context in the parent component
Use UserContext.Provider to wrap the child components and pass the user object as the value prop.
Astro
Hint
Wrap the child components inside <UserContext.Provider value={user}> and close it properly.
4
Consume the user context in child components
In two separate components, use UserContext.useContext() to get the user object and display user.name in UserName and user.role in UserRole.
Astro
Hint
Use const user = UserContext.useContext(); inside each component and return a paragraph showing the correct user property.
Practice
(1/5)
1. What is the main purpose of using createContext in Astro components?
easy
A. To share user data easily across multiple components
B. To create a new component
C. To style components with CSS
D. To fetch data from an API
Solution
Step 1: Understand what createContext does
createContext creates a shared space to hold data accessible by many components.
Step 2: Identify the purpose related to user data
It is used to share user information easily without passing props manually through every component.
Final Answer:
To share user data easily across multiple components -> Option A
Quick Check:
User context shares data = A [OK]
Hint: Remember: createContext sets shared data space [OK]
Common Mistakes:
Thinking createContext creates components
Confusing context with styling or API calls
Assuming it fetches data automatically
2. Which of the following is the correct way to read user context inside an Astro component?
easy
A. const user = useState(UserContext);
B. const user = useContext(UserContext);
C. const user = Provider(UserContext);
D. const user = createContext(UserContext);
Solution
Step 1: Identify the function to read context
useContext is the hook used to access context data inside components.
Step 2: Check the syntax correctness
The correct syntax is const user = useContext(UserContext); to get the user data.
Final Answer:
const user = useContext(UserContext); -> Option B
Quick Check:
Reading context uses useContext = D [OK]
Hint: Use useContext to read context data inside components [OK]
Common Mistakes:
Using createContext to read data
Confusing Provider with reading context
Using useState instead of useContext
3. Given this Astro component code snippet:
const UserContext = createContext();
function UserProfile() {
const user = useContext(UserContext);
return <div>Hello, {user.name}!</div>;
}
function App() {
return (
<UserContext.Provider value={{ name: 'Alice' }}>
<UserProfile />
</UserContext.Provider>
);
}
What will be rendered on the page?
medium
A. Error: useContext called outside Provider
B. Hello, undefined!
C. Hello, user!
D. Hello, Alice!
Solution
Step 1: Check the Provider value
The UserContext.Provider wraps UserProfile and provides { name: 'Alice' } as context.
Step 2: Check how UserProfile uses context
UserProfile reads the context with useContext(UserContext) and accesses user.name.
Final Answer:
Hello, Alice! -> Option D
Quick Check:
Provider value passed correctly = C [OK]
Hint: Provider value sets user data; useContext reads it [OK]
Common Mistakes:
Forgetting to wrap component in Provider
Expecting error when Provider is present
Using wrong property name for user
4. Identify the error in this Astro component code:
const UserContext = createContext();
function UserProfile() {
const user = useContext(UserContext);
return <div>Welcome, {user.name}!</div>;
}
function App() {
return (
<UserProfile />
);
}
What is the problem?
medium
A. useContext is used incorrectly; should be useState
B. UserContext is not created properly
C. UserProfile is not wrapped in UserContext.Provider, so context is undefined
D. UserProfile should not return JSX
Solution
Step 1: Check if Provider wraps UserProfile
The UserProfile component uses useContext(UserContext) but is not inside a UserContext.Provider.
Step 2: Understand the effect of missing Provider
Without the Provider, the context value is undefined, so user.name will cause an error or be undefined.
Final Answer:
UserProfile is not wrapped in UserContext.Provider, so context is undefined -> Option C
Quick Check:
Missing Provider causes undefined context = B [OK]
Hint: Always wrap consumers in Provider to avoid undefined context [OK]
Common Mistakes:
Assuming useContext works without Provider
Confusing useContext with useState
Thinking JSX return is invalid
5. You want to create a user context that provides both name and role to nested components. Which code correctly sets up the context and accesses both values inside a child component?
hard
A.
const UserContext = createContext();
function UserProvider({ children }) {
return (
<UserContext.Provider value={{ name: 'Bob', role: 'admin' }}>
{children}
</UserContext.Provider>
);
}
function Dashboard() {
const user = useContext(UserContext);
return <div>User: {user.name}, Role: {user.role}</div>;
}