main
js 40 lines 927 Bytes
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 import * as React from 'react';
9 import {useLayoutEffect, useRef, useState} from 'react';
10 import {createRoot} from 'react-dom/client';
11
12 function createContainer() {
13 const container = document.createElement('div');
14
15 ((document.body: any): HTMLBodyElement).appendChild(container);
16
17 return container;
18 }
19
20 function EffectWithState() {
21 const [didMount, setDidMount] = useState(0);
22
23 const renderCountRef = useRef(0);
24 renderCountRef.current++;
25
26 useLayoutEffect(() => {
27 if (!didMount) {
28 setDidMount(true);
29 }
30 }, [didMount]);
31
32 return (
33 <ul>
34 <li>Rendered {renderCountRef.current} times</li>
35 {didMount && <li>Mounted!</li>}
36 </ul>
37 );
38 }
39
40 createRoot(createContainer()).render(<EffectWithState />);