Junior #state#props
State và props khác nhau thế nào?
⚡ Trả lời nhanh
Props là dữ liệu cha truyền xuống (chỉ đọc); state là dữ liệu nội bộ component tự quản lý và thay đổi bằng setter, thay đổi sẽ gây re-render.
function Counter({ step }) { // props: step (chỉ đọc)
const [n, setN] = useState(0); // state: n (tự quản lý)
return <button onClick={() => setN(n + step)}>{n}</button>;
}
<Counter step={2} />