您需要在 useEffect
Hook 的第二个参数中添加什么以将其限制为仅在第一次渲染时运行?
import { useState, useEffect } from "react";
import ReactDOM from "react-dom/client";
function App() {
const [data, setData] = useState([]);
useEffect(() => {
setData(getData())
}, @(2));
return <DisplayData data={data} />;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
import { useState, useEffect } from "react";
import ReactDOM from "react-dom/client";
function App() {
const [data, setData] = useState([]);
useEffect(() => {
setData(getData())
}, []);
return <DisplayData data={data} />;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);