Consider an Astro component tree where a parent component sets a user context. How does this context behave in nested child components?
<!-- Parent.astro --> --- import Child from './Child.astro'; const user = { name: 'Alice' }; Astro.provideContext('user', user); --- <div> <Child /> </div> <!-- Child.astro --> --- const user = Astro.useContext('user'); --- <p>User: {user?.name}</p>
Think about how Astro's provideContext and useContext work for passing data down the component tree.
Astro's provideContext passes data down to nested components. The child component uses useContext to access the same user object, so it can display the user's name.
Given a parent Astro component that updates user context dynamically, what will the child component display after the update?
<!-- Parent.astro --> --- import Child from './Child.astro'; let user = { name: 'Bob' }; Astro.provideContext('user', user); user.name = 'Charlie'; --- <div> <Child /> </div> <!-- Child.astro --> --- const user = Astro.useContext('user'); --- <p>User: {user.name}</p>
Consider if Astro context is reactive or just a reference to the object.
Astro context passes the object reference. Changing the object's property updates the value seen by child components, so the child shows 'Charlie'.
Identify the option that correctly sets and accesses user context in Astro components without syntax errors.
Check for missing semicolons and safe access to properties.
Option A correctly uses semicolons and safe optional chaining user?.name to avoid errors if context is missing.
Given the code below, why does the child component display 'User: undefined'?
<!-- Parent.astro --> --- import Child from './Child.astro'; const user = { name: 'Eve' }; --- <div> <Child /> </div> <!-- Child.astro --> --- const user = Astro.useContext('user'); --- <p>User: {user?.name}</p>
Check if the parent component actually provides the context key used by the child.
The parent component did not call Astro.provideContext('user', user), so the child cannot find the 'user' context and gets undefined.
In an Astro app, if a parent and a nested child component both call Astro.provideContext('user', ...) with different values, what will a grandchild component receive when calling Astro.useContext('user')?
Think about how context scopes work in component trees.
Astro context works like scopes: the closest provider for a context key shadows any outer providers. So the grandchild gets the child's 'user' context value.
