Passing Props to a Component – React - https://react.dev/learn/passing-props-to-a-component
props:
- info pass from parent
- info pass by jsx tag
- read from function({xx,xxx})
- read only
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
function App() {
const [users, setUsers] = useState();
// Function to collect data
const getApiData = async () => {
const response = await fetch(
"https://jsonplaceholder.typicode.com/todos/"
).then((response) => response.json());
setUsers(response);
};
useEffect(() => {
getApiData();
}, []);
return (
<div className="app">
{users &&
users.map((user) => (
<div className="item-container">
Id:{user.id} <div className="title">Title:{user.title}</div>
</div>
))}
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
|
1
2
3
4
5
6
7
|
useEffect(() => {
// side effect code here
return () => {
// cleanup code here
};
}, [dependencies]);
|
执行副作用
when:
- after first render
- the dependencies have changed
warning: has missing dependencies:
依赖了外部 variable/function 却没有加入依赖之中
solve:
- 添加依赖
- 将依赖放入 effect 方法中
export