Files
component-party/content/4-component-composition/5-context/react/App.jsx
2024-10-27 14:08:38 +01:00

25 lines
583 B
JavaScript

import { useState } from "react";
import UserProfile from "./UserProfile";
import { UserContext } from "./UserContext";
export default function App() {
const [user, setUser] = useState({
id: 1,
username: "unicorn42",
email: "unicorn42@example.com",
});
function updateUsername(newUsername) {
setUser((userData) => ({ ...userData, username: newUsername }));
}
return (
<>
<h1>Welcome back, {user.username}</h1>
<UserContext.Provider value={{ ...user, updateUsername }}>
<UserProfile />
</UserContext.Provider>
</>
);
}