main
js 117 lines 2.92 KB
Raw
1 import * as React from 'react';
2
3 const {
4 useState,
5 useEffect,
6 useCallback,
7 useContext,
8 createContext,
9 memo,
10 forwardRef,
11 } = React;
12
13 // A custom hook, to exercise the hooks tree in react_get_component_by_uid.
14 function useToggle(initial) {
15 const [on, setOn] = useState(initial);
16 const toggle = useCallback(() => setOn(value => !value), []);
17 return [on, toggle];
18 }
19
20 // Function component with a single State hook + a button that triggers
21 // re-renders (useful for profiling).
22 function Counter() {
23 const [count, setCount] = useState(0);
24 return (
25 <div className="row">
26 <span>Count: {count}</span>
27 <button onClick={() => setCount(value => value + 1)}>+1</button>
28 </div>
29 );
30 }
31
32 // Uses the custom hook above, so its hooks tree nests a "Toggle" custom hook.
33 function Toggle() {
34 const [on, toggle] = useToggle(false);
35 return <button onClick={toggle}>{on ? 'ON' : 'OFF'}</button>;
36 }
37
38 // State + Effect hook with a timer — re-renders every second, handy for
39 // react_start_profiling / react_get_trace_overview / react_get_commit_report.
40 function Clock() {
41 const [now, setNow] = useState(() => new Date().toLocaleTimeString());
42 useEffect(() => {
43 const id = setInterval(() => setNow(new Date().toLocaleTimeString()), 1000);
44 return () => clearInterval(id);
45 }, []);
46 return <div className="row">Clock: {now}</div>;
47 }
48
49 // Context, to exercise the "context" node type and useContext.
50 const ThemeContext = createContext('light');
51
52 function ThemeProvider({children}) {
53 return <ThemeContext.Provider value="dark">{children}</ThemeContext.Provider>;
54 }
55
56 function ThemedPanel() {
57 const theme = useContext(ThemeContext);
58 return (
59 <section className="panel" data-theme={theme}>
60 <h2>Panel (theme: {theme})</h2>
61 <Counter />
62 <Toggle />
63 <Clock />
64 </section>
65 );
66 }
67
68 // List with keys, to exercise key reporting and find_components pagination.
69 function Todo({text}) {
70 return <li>{text}</li>;
71 }
72
73 function TodoList() {
74 const items = [
75 {id: 'learn', text: 'Learn the tools'},
76 {id: 'test', text: 'Test the fixture'},
77 {id: 'ship', text: 'Ship the package'},
78 ];
79 return (
80 <ul className="todos">
81 {items.map(item => (
82 <Todo key={item.id} text={item.text} />
83 ))}
84 </ul>
85 );
86 }
87
88 // memo and forwardRef, to exercise the "memo" and "forwardRef" node types.
89 const MemoBox = memo(function MemoBox({label}) {
90 return <div className="box">Memo: {label}</div>;
91 });
92
93 const FancyInput = forwardRef(function FancyInput({placeholder}, ref) {
94 return <input ref={ref} placeholder={placeholder} />;
95 });
96
97 function Header() {
98 return (
99 <header>
100 <h1>react-devtools-cdt-mcp fixture</h1>
101 </header>
102 );
103 }
104
105 export default function App() {
106 return (
107 <main className="app">
108 <Header />
109 <ThemeProvider>
110 <ThemedPanel />
111 </ThemeProvider>
112 <TodoList />
113 <MemoBox label="hello" />
114 <FancyInput placeholder="type here" />
115 </main>
116 );
117 }