前言:
紀錄一下 React
筆記。
參考資料:
React will create React Tree, it make React Dom by JSX
Recap
- React will preserve state when position not change(React Dom Tree not change place)
- If you want to reset or update you can give key or put component in different structure
- If you destroy component,React will reset state value
- We should not contain components in another components
This is because inside components every time will cause reRender issue and otherwise state will not keep anymore
This leads to bugs and performance problems.
Just like belowcodesendbox1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26import { useState } from 'react';
export default function MyComponent() {
const [counter, setCounter] = useState(0);
function MyTextField() {
const [text, setText] = useState('');
return (
<input
value={text}
onChange={e => setText(e.target.value)}
/>
);
}
return (
<>
<MyTextField />
<button onClick={() => {
setCounter(counter + 1)
}}>Clicked {counter} times</button>
</>
);
} - Don’t nest component definitions, or you’ll reset state by accident.