Consider the initial example from the lecture where user data is passed through props to <UserDetails>, <UserInfo>, and <UserPosts>. Imagine you’re building a larger app with 10 nested components, and 5 of them need access to the user object. Why might prop drilling become a problem here?
user
<UserDetails>
<UserInfo>
<UserPosts>
It makes the code harder to maintain as you add more components.
It prevents you from using hooks like useState.
useState
It forces every component to re-render even if they don’t use the user data.
It’s slower than using Context API in terms of performance.
It increases the risk of passing incorrect props to the wrong component.
Look at this code snippet from the lecture:
const UserContext = createContext(); export const UserProvider = ({ children }) => { const [user, setUser] = useState("John Doe"); return ( <UserContext.Provider value={{ user, setUser }}> {children} </UserContext.Provider> ); };
You’re tasked with adding a new feature: tracking the user’s login status. How could you modify the UserProvider to include this?
UserProvider
Update the value prop to { user, setUser, isLoggedIn: true } without using state.
value
{ user, setUser, isLoggedIn: true }
Create a separate LoginContext for the login status.
LoginContext
Add const [isLoggedIn, setIsLoggedIn] = useState(false) and include it in the value prop.
const [isLoggedIn, setIsLoggedIn] = useState(false)
Replace user with an object like { name: "John Doe", isLoggedIn: false }.
{ name: "John Doe", isLoggedIn: false }
Pass isLoggedIn as a prop to <UserProvider> instead of using state.
isLoggedIn
<UserProvider>
In the custom hook example:
export const useUser = () => { const context = useContext(UserContext); if (!context) { throw new Error("useUser must be used within a UserProvider"); } return context; };
A student tries to use useUser() in a component outside the <UserProvider>. What will happen, and why is this behavior useful?
useUser()
React automatically uses a default value from createContext().
createContext()
The app crashes with an error message about missing context.
It triggers an infinite re-render loop.
The useUser hook returns an empty object {}.
useUser
{}
The component renders undefined for the user value silently.
undefined
Imagine a scenario: You’re building a blog app where the theme (light/dark) and user info are shared across components. You decide to use Context API. Based on the lecture, which of these approaches would work effectively? (Select all that apply.)
Pass theme and user data as props to every component instead of using Context.
Create one AppContext with { theme, setTheme, user, setUser }.
AppContext
{ theme, setTheme, user, setUser }
Create separate ThemeContext and UserContext with their own providers.
ThemeContext
UserContext
Use a custom hook useAppState to access a single context with all data.
useAppState
Nest <ThemeProvider> and <UserProvider> in the app root.
<ThemeProvider>
Examine this component using the custom hook:
const UserProfile = () => { const { user, setUser } = useUser(); return ( <div> <h1>User: {user}</h1> <button onClick={() => setUser("Jane Doe")}>Change User</button> </div> ); };
A teammate suggests adding a feature to log the user out by setting user to null. What potential issues might arise?
null
The component won’t re-render because null isn’t a valid state update.
The useUser hook will throw an error because null isn’t allowed.
The <UserProvider> will reset to its initial state automatically.
It could break components expecting user to always be a string.
user.name might throw an error if accessed when user is null.
user.name
This content is only available to registered users.Please register at the the home page.