Contents

react

Passing Props to a Component – React - https://react.dev/learn/passing-props-to-a-component

props:

  1. info pass from parent
  2. info pass by jsx tag
  3. read from function({xx,xxx})
  4. 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:

  1. after first render
  2. the dependencies have changed

warning: has missing dependencies: 依赖了外部 variable/function 却没有加入依赖之中

solve:

  1. 添加依赖
  2. 将依赖放入 effect 方法中

export