forked from matschik/component-party.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Component composition > Context for Qwik (matschik#155)
- Loading branch information
1 parent
421ac31
commit baec35a
Showing
3 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
component$, | ||
useStore, | ||
useContextProvider, | ||
createContext, | ||
$, | ||
} from "@builder.io/qwik"; | ||
import UserProfile from "./UserProfile"; | ||
|
||
export const UserContext = createContext("user-context"); | ||
|
||
const App = component$(() => { | ||
const user = useStore({ | ||
id: 1, | ||
username: "unicorn42", | ||
email: "[email protected]", | ||
}); | ||
|
||
const updateUsername = $((newUsername) => { | ||
user.username = newUsername; | ||
}); | ||
|
||
useContextProvider(UserContext, { user, updateUsername }); | ||
|
||
return ( | ||
<> | ||
<h1>Welcome back, {user.username}</h1> | ||
<UserProfile /> | ||
</> | ||
); | ||
}); | ||
|
||
export default App; |
19 changes: 19 additions & 0 deletions
19
content/4-component-composition/5-context/qwik/UserProfile.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { component$, useContext } from "@builder.io/qwik"; | ||
import { UserContext } from "./App"; | ||
|
||
const UserProfile = component$(() => { | ||
const { user, updateUsername } = useContext(UserContext); | ||
|
||
return ( | ||
<div> | ||
<h2>My Profile</h2> | ||
<p>Username: {user.username}</p> | ||
<p>Email: {user.email}</p> | ||
<button onClick$={() => updateUsername("Jane")}> | ||
Update username to Jane | ||
</button> | ||
</div> | ||
); | ||
}); | ||
|
||
export default UserProfile; |