反應鉤子,不會將選中的屬性更改為複選框 (React hooks, does not change the checked property to checkbox)


問題描述

反應鉤子,不會將選中的屬性更改為複選框 (React hooks, does not change the checked property to checkbox)

我正在嘗試更改 ckeckbox 屬性,每次向地圖寫入新值時,在我單擊複選框時,它只會更改選中屬性的值,例如,初始值為是的,在所有後續點擊中,它將是錯誤的、錯誤的、錯誤的......這裡有什麼問題?

import React,{ useState,useEffect } from "react";
import {useSelector} from 'react‑redux'

const ChangeArticle = (props) => {

    const prevArticle = props.changeArtcle;
    const age_groups = useSelector(state => state.app.age_groups);
    const [checkedAges, setCheckAges] = useState(new Map());

    const handleChangeCheckBoxe = (event) => {

        setCheckAges(checkedAges => checkedAges.set(event.target.value, event.target.checked));
        console.log("checkedItems: ", checkedAges);

    }

    useEffect(() => {

        if(prevArticle.ages){
            prevArticle.ages.forEach((age) =>{
                setCheckAges(checkedAges => checkedAges.set(age.toString(), true));
            });
        }


    },[prevArticle]);

    return (<div>

            {age_groups.map(age => {

                return (<div key={age.id}>
                     <input type="checkbox" checked={checkedAges.has(age.id.toString()) ? checkedAges.get(age.id.toString()) : false} value={age.id} onChange={handleChangeCheckBoxe}
                />
                { age.title }
                </div>) 

            }) }
    </div>);
}

export default ChangeArticle;

參考解法

方法 1:

In the handleChangeCheckBoxe function, you are only changing the values withing the Map. React only does a shallow reference check to see if the Map had changed. Since the reference is the same, the it will not re‑render the component as you would expect.

You can change the function to be similar to the following to create a new Map and assign it to state.

  const handleChangeCheckBoxe = (event) => {
      setCheckAges(checkedAges => new Map(checkedAges.set(event.target.value, event.target.checked)));
      console.log("checkedItems: ", checkedAges);
  }

You can see this working in the following code sandbox https://codesandbox.io/s/wispy‑leftpad‑9sji0?fontsize=14&hidenavigation=1&theme=dark

(by Bob KapusAkbanipuna777)

參考文件

  1. React hooks, does not change the checked property to checkbox (CC BY‑SA 2.5/3.0/4.0)

#react-hooks #reactjs






相關問題

使用反應鉤子useState更新功能的正確方法是什麼? (What is the correct way to use react hook useState update function?)

測試 react-redux useSelector (testing react-redux useSelector)

從深度嵌套的組件更新狀態而不重新渲染父組件 (Update state from deeply nested component without re-rendering parents)

React Hook useEffect 缺少依賴項(在上下文中定義的函數) (React Hook useEffect has a missing dependency (function defined in context))

類型“IntrinsicAttributes”(自定義掛鉤)上不存在 React 屬性 (React Property does not exist on type 'IntrinsicAttributes' (custom hook))

即使驗證要求無效,數據仍在發送,解決此問題的最佳方法是什麼? (Data is sending even if validation requirements are not valid, whats the best way to approach this?)

如何在使用狀態掛鉤更新狀態時覆蓋具有相同鍵的對象 (How can overwrite objects that have the same key in updating State with state hooks)

在 useEffect 中,調用更新時是否會更新所有變量? (In useEffect, do all variables get updated when an update is called?)

反應鉤子,不會將選中的屬性更改為複選框 (React hooks, does not change the checked property to checkbox)

在 reactJS 中動態設置圖像 src (Setting image src dynamically in reactJS)

如何防止組件在反應中重新渲染? (How to prevent component from re-rendering in react?)

使用 useEffect 和 setInterval 一個接一個地打印一個字母 (print one letter after the other with useEffect and setInterval)







留言討論