Skip to content

Commit

Permalink
Add Component composition > Context for Qwik (matschik#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnsonMao authored Mar 7, 2023
1 parent 421ac31 commit baec35a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ How do we solve this ? Developers love having framework overview by examples. It
- [x] Lifecycle
- [x] On mount
- [x] On unmount
- [ ] Component composition
- [x] Component composition
- [x] Props
- [x] Emit to parent
- [x] Slot
- [x] Slot fallback
- [ ] Context
- [x] Context
- [x] Form input
- [x] Input text
- [x] Checkbox
Expand Down
33 changes: 33 additions & 0 deletions content/4-component-composition/5-context/qwik/App.tsx
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 content/4-component-composition/5-context/qwik/UserProfile.tsx
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;

0 comments on commit baec35a

Please sign in to comment.