stateを持ち上げる
React入門 / 全 6 枚
子で持つか、親で持つか
兄弟それぞれが自分の state を持つと、値がずれます。共通の親へ移します。
子がそれぞれ持つ
function FilterButtons() {
const [category, setCategory] =
useState("all");
// ここだけの値になる
}共通の親が持つ
function App() {
const [category, setCategory] =
useState("all");
// 両方の子へ props で配る
}同じ意味の値を2か所に置かないのが鉄則です。
1 / 6
← → キーでも送れます