Created
February 2, 2026 18:41
-
-
Save Lysak/4f491c15c2beec24b001c214287fbf92 to your computer and use it in GitHub Desktop.
react-live-coding.ts
This file contains hidden or 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
| import { useState, useEffect } from "react"; | |
| const useUserData = (userId) => { | |
| const [user, setUser] = useState(null); | |
| useEffect(() => { | |
| const fetchUser = async () => { | |
| const response = await fetch(`https://api.example.com/users/${userId}`); | |
| const data = await response.json(); | |
| setUser(data); | |
| }; | |
| fetchUser(); | |
| }, [userId]); | |
| return user; | |
| }; | |
| export const UserProfile = ({ userId }) => { | |
| const user = useUserData(userId); | |
| const [count, setCount] = useState(0); | |
| const permissionOptions = { | |
| isAdmin: false, | |
| updatesAllowed: true, | |
| notification: true, | |
| }; | |
| const handleUpdate = () => { | |
| // Simulating an expensive calculation or update | |
| setCount(count + 1); | |
| console.log("Profile updated for:", user.name); | |
| }; | |
| return ( | |
| <div> | |
| <h1>{user.name}</h1> | |
| <p>Email: {user.email}</p> | |
| <button onClick={handleUpdate}>Refresh Local State: {count}</button> | |
| <CustomComponent | |
| userName={user.name} | |
| permissionOptions={permissionOptions} | |
| /> | |
| </div> | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment