useEffectはレンダーの後で呼ばれる
import React, { useEffect, useState } from "react";
const App = () => {
const [num, setNum] = useState(3);
return (
<div>
<button onClick={() => setNum(num + 1)}>{num}</button>
<List num={num} />
</div>
);
};
const List = ({ num }) => {
const [list, setList] = useState([]);
useEffect(() => {
let _list = [];
for (let i = 0; i < num; i++) {
_list.push("DEBUG:" + _list.length);
}
setList(_list);
}, [num]);
console.log({ list });
return (
<div>
{list.map((v, i) => {
return <p key={i}>{v}</p>;
})}
</div>
);
};
export default App;