Replies: 1 comment
-
|
useContext allows you to avoid prop drilling; this is useful on the server. Say you have an AuthProvider and an AuthStatus component. You can nest that AuthStatus component deeply inside a component hierarchy and it will still know the current auth context from the provider. AuthStatus.tsx export AuthContext = () => {
const { isLoggedIn, login, logout } = useContext(AuthContext)
return isLoggedIn ? <Button onClick={logout}>Logout</Button> : <Button onClick={login}>Login</Button>
}NavBar.tsx export NavBar = () => {
return <nav>
<AuthStatus />
</nav>
}main.tsx <AuthProvider>
<Layout>
<NavBar>
<main>...</main>
</Layout>
</AuthProvider>So even though my AuthStatus is nested inside my NavBar which is nested in my main, I don't need to do any prop drilling. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I was going thru the Hono docs when building a site and noticed that it has a useContext that can be called on the server? Am I reading this correctly? Because React useContext is purely a client side hook. How exactly does Hono save the context on the server? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions