MCPcopy Create free account
hub / github.com/cosdensolutions/code / Demo

Function Demo

videos/long/learn-react-hooks-useLayoutEffect/index.tsx:11–37  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9const userIds = [1, 2];
10
11const Demo = () => {
12 const [userId, setUserId] = useState(userIds[0]);
13 const [isAdmin, setIsAdmin] = useState(false);
14
15 // This artificially slows down rendering
16 let now = performance.now();
17 while (performance.now() - now < 200) {
18 // Do nothing for a bit...
19 }
20
21 useLayoutEffect(() => {
22 setIsAdmin(userId === userIds[0]);
23 }, [userId]);
24
25 const handleChange = () => {
26 const otherId = userIds.find((id) => id !== userId)!;
27 setUserId(otherId);
28 };
29
30 return (
31 <div className='tutorial-shorts'>
32 <p>userId: {userId}</p>
33 <p>Admin: {isAdmin}</p>
34 <Button title='Change User' onClick={handleChange} />
35 </div>
36 );
37};
38
39export default Demo;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected