Javascript Reactjs——基于复选框的选中/取消选中发出api请求未正确理解

Javascript Reactjs——基于复选框的选中/取消选中发出api请求未正确理解,javascript,reactjs,checkbox,Javascript,Reactjs,Checkbox,我在reactJS中有一段代码,它将在将来基于check/uncheck发出api请求。 现在,它只是基于数组中复选框的选中/取消选中来推/删除categoryId 该代码是完美的,工作良好,但我无法理解一些地方的代码。 请帮助我理解它 代码:: import React, {useState, useEffect} from 'react'; const Checkbox = ({categories}) => { const [checked, setChecked] = u

我在reactJS中有一段代码,它将在将来基于check/uncheck发出api请求。 现在,它只是基于数组中复选框的选中/取消选中来推/删除categoryId

该代码是完美的,工作良好,但我无法理解一些地方的代码。 请帮助我理解它

代码::

import React, {useState, useEffect} from 'react';

const Checkbox = ({categories}) => {
    const [checked, setChecked] = useState([]);

    const handleToggle = c => () => {
        // returns the first index or -1
        const currentIndex = checked.indexOf(c);
        const newCheckedCategoryArray = [...checked];

        if(currentIndex === -1){
            //then push in default state or remove it if its already there.
            newCheckedCategoryArray.push(c);
        }
        else{
            newCheckedCategoryArray.splice(currentIndex, 1)
        }
        setChecked(newCheckedCategoryArray);
        console.log(newCheckedCategoryArray);
    }

    return categories.map((c, i) => (
        <li className="list-unstyled" key={i}>
            <input type="checkbox" value={checked.indexOf(c._id === -1)}
                className="form-check-input" onChange={handleToggle(c._id)}
                 />
            <label className="form-check-label">{c.name}</label>
        </li>
    ));

}
export default Checkbox;
import React,{useState,useffect}来自“React”;
常量复选框=({categories})=>{
const[checked,setChecked]=useState([]);
const handleToggle=c=>()=>{
//返回第一个索引或-1
const currentIndex=checked.indexOf(c);
const newCheckedCategoryArray=[…已检查];
如果(当前索引==-1){
//然后在默认状态下推送或删除它(如果已经存在)。
新检查的类别。推送(c);
}
否则{
newCheckedCategoryArray.splice(当前索引,1)
}
setChecked(newCheckedCategoryArray);
console.log(newCheckedCategoryArray);
}
返回类别.map((c,i)=>(
  • {c.name}
  • )); } 导出默认复选框;
    我不理解的代码如下: “价值”属性逻辑的目的是什么

     <input type="checkbox" value={checked.indexOf(c._id === -1)}
                    className="form-check-input" onChange={handleToggle(c._id)}
                     />
    
    
    
    检查输入时的type='checkbox'

    简短而甜蜜的版本:

    [值属性是]表示复选框值的DOMString。这在客户端上是看不到的,但在服务器上,这是使用复选框名称提交的数据的值

    您显示的代码并不表示value属性正在任何地方使用,因此它不会给您带来任何问题;你可以移除它,它仍然可以正常工作。(假设值未在其他地方、其他函数中使用)

    顺便问一下,你确定这是完美的工作吗?在我看来你可能有打字错误。我认为

    <input type="checkbox" value={checked.indexOf(c._id === -1)}
                className="form-check-input" onChange={handleToggle(c._id)}
                 />
    
    
    
    应写为

    <input type="checkbox" value={checked.indexOf(c._id) === -1}
                className="form-check-input" onChange={handleToggle(c._id)}
                 />The code you wrote had a T/F statement inside of "indexOf," so you would only ever have searched the checked array for the first instance of a boolean true or a boolean false. 
    
    您编写的代码在“indexOf”中有一个T/F语句,因此您只能在选中的数组中搜索布尔true或布尔false的第一个实例。
    

    我编写的代码将检查您提交的id是否在已检查数组中。如果它不在选中的数组中,它将返回true。如果它在选中的数组中,它将返回fales。

    请检查以下示例:

    “蓝鲸”。indexOf('Blue')!=-1; // 真的 “蓝鲸”。indexOf('Bloe')!==-1; // 假的

    就你而言:: “蓝鲸”。indexOf('Blue')=-1//假的 它将检查找到的索引是否等于-1。 如果为-1,则计算为true,否则为false。

    有几件事。1) 为什么不问问编写代码的人它的目的是什么?2) 从技术上讲,它没有预定义的用途,因为
    不是
    复选框
    的有效属性。