Share the Signed-in User
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Give any component the signed-in user without passing it down as a prop. Export a UserProvider component with props initialUser (an object such as { name: "Ada" }, or null for nobody; null when left out) and children, and a useUser hook that returns an object { user, signOut }. user starts as initialUser, and signOut() sets it to null for every component inside the same provider. Called outside any UserProvider, useUser must throw an Error with the message "useUser must be used inside a UserProvider", so a missing provider shows up straight away instead of as a confusing blank. The default export App is a page with a header and a welcome message for the preview; the checks only use UserProvider and useUser.
Your code must pass
- useUser gives the user to components inside the provider
- signOut clears the user for every component
- useUser outside a UserProvider throws a clear error