将状态false设置为地图组件中的检查收音机按钮

发布于 2025-02-12 05:47:24 字数 1338 浏览 0 评论 0原文

function EachData({ data, index, open }) {
  const [isOpen, setisOpen] = useState(open);

  const toggle = (e) => {
    setisOpen((prev) => !prev);      
  };

  return (
    <tr>
      {/*<td></td>'s ... */}
      <td>
        <div className="functions">
          {!isOpen ? (
            <>
              <label className="far fa-edit" htmlFor={`label-${index + 1}`}>
                <input type="radio" name="edit" id={`label-${index + 1}`} onChange={toggle} />
              </label>
              <label className="far fa-trash"></label>
            </>
          ) : (
            <>
              <label className="far fa-circle-check"></label>
              <label className="far fa-times-circle" htmlFor={`label-${index + 1}`} >
                <input type="radio" name="edit" id={`label-${index + 1}`} onChange={toggle} />
              </label>
            </>
          )}
        </div>
      </td>
    </tr>
  );
}

export default EachData;

App.js

array.map((data, index)=>{
  return(
    <EachData data={data} index={index} isOpen={false}/>
  )
})

检查无线电按钮时,JSX按预期更改,但是在检查另一个无线电按钮后,前一个人的状态仍然是正确的。如何将这些元素陈述为false?

function EachData({ data, index, open }) {
  const [isOpen, setisOpen] = useState(open);

  const toggle = (e) => {
    setisOpen((prev) => !prev);      
  };

  return (
    <tr>
      {/*<td></td>'s ... */}
      <td>
        <div className="functions">
          {!isOpen ? (
            <>
              <label className="far fa-edit" htmlFor={`label-${index + 1}`}>
                <input type="radio" name="edit" id={`label-${index + 1}`} onChange={toggle} />
              </label>
              <label className="far fa-trash"></label>
            </>
          ) : (
            <>
              <label className="far fa-circle-check"></label>
              <label className="far fa-times-circle" htmlFor={`label-${index + 1}`} >
                <input type="radio" name="edit" id={`label-${index + 1}`} onChange={toggle} />
              </label>
            </>
          )}
        </div>
      </td>
    </tr>
  );
}

export default EachData;

App.js

array.map((data, index)=>{
  return(
    <EachData data={data} index={index} isOpen={false}/>
  )
})

When I check the radio buttons the jsx changes as expected, but after checking another radio button the previous one's state remains true. How do I set those elements state to false ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

大海や 2025-02-19 05:47:24

您应该将等级状态存储在array数据中,而不是everydata component中

const [array, setArray] = useState();

const toggle = idx => {
    const newArray = array.map((item, index) => {
        if (idx == index) return {
           ...item,
           isOpen: !item.isOpen
        }
        return item
    })
    setArray(newArray);
}

array.map((data, index) => {
    return <EachData data={data} index={index} isOpen={data.isOpen} toggle={toggle} />;
});

function EachData({ data, index, isOpen, toggle }) {
    return (
        <tr>
            {/*<td></td>'s ... */}
            <td>
                <div className="functions">
                    {!isOpen ? (
                        <>
                            <label className="far fa-edit" htmlFor={`label-${index + 1}`}>
                                <input
                                    type="radio"
                                    name="edit"
                                    id={`label-${index + 1}`}
                                    onChange={() => toggle(index)}
                                />
                            </label>
                            <label className="far fa-trash"></label>
                        </>
                    ) : (
                        <>
                            <label className="far fa-circle-check"></label>
                            <label className="far fa-times-circle" htmlFor={`label-${index + 1}`}>
                                <input
                                    type="radio"
                                    name="edit"
                                    id={`label-${index + 1}`}
                                    onChange={() => toggle(index)}
                                />
                            </label>
                        </>
                    )}
                </div>
            </td>
        </tr>
    );
}

You should store isOpen state in your array data, not in EachData component

const [array, setArray] = useState();

const toggle = idx => {
    const newArray = array.map((item, index) => {
        if (idx == index) return {
           ...item,
           isOpen: !item.isOpen
        }
        return item
    })
    setArray(newArray);
}

array.map((data, index) => {
    return <EachData data={data} index={index} isOpen={data.isOpen} toggle={toggle} />;
});

function EachData({ data, index, isOpen, toggle }) {
    return (
        <tr>
            {/*<td></td>'s ... */}
            <td>
                <div className="functions">
                    {!isOpen ? (
                        <>
                            <label className="far fa-edit" htmlFor={`label-${index + 1}`}>
                                <input
                                    type="radio"
                                    name="edit"
                                    id={`label-${index + 1}`}
                                    onChange={() => toggle(index)}
                                />
                            </label>
                            <label className="far fa-trash"></label>
                        </>
                    ) : (
                        <>
                            <label className="far fa-circle-check"></label>
                            <label className="far fa-times-circle" htmlFor={`label-${index + 1}`}>
                                <input
                                    type="radio"
                                    name="edit"
                                    id={`label-${index + 1}`}
                                    onChange={() => toggle(index)}
                                />
                            </label>
                        </>
                    )}
                </div>
            </td>
        </tr>
    );
}
你的他你的她 2025-02-19 05:47:24

您应该使用useref而不是ID,在这里我使用useref进行一些逻辑,希望这会有所帮助。

根据我的理解,当您单击MAP功能中的单击“广播”按钮时,这将被激活,当您单击另一个无线电按钮时,上一个单选按钮仍显示为活动,在此代码中,我创建两个USEREF如您所见,如下代码,所有indexes都采用了第二个代码,第二个用于删除先前的无线电按钮活动。希望您了解此代码,如果您知道DOM。

function EachData({ data, index, open }) {
  const [isOpen, setisOpen] = useState(open);

  const radioRef = useRef([]);
  const previousRadioRef = useRef([]);

  const toggle = (i) => {
    setisOpen((prev) => !prev);
    if (previousRadioRef.current && previousRadioRef.current[0] !== radioRef.current[i]) {
      if (radioRef.current[i]) {
        if (previousRadioRef.current.length) {
        previousRadioRef.current[0].checked = false;
        previousRadioRef.current = [];
      }
      radioRef.current[i].checked = true;
      previousRadioRef.current.push(radioRef.current[i]);
    }
    } else if(previousRadioRef.current && previousRadioRef.current[0]) {
       previousRadioRef.current[0].checked = false;
       previousRadioRef.current = [];
   }
  };

  return (
    <>
      <tr>
      <td>
        <div className="functions">
          {!isOpen ? (
            <>
              <label className="far fa-edit">
                <input type="radio" name="edit" ref={ref => (radioRef.current[index] = ref)} onChange={() => toggle(index)} />
              </label>
              <label className="far fa-trash"></label>
            </>
          ) : (
            <>
              <label className="far fa-circle-check"></label>
              <label className="far fa-times-circle">
                <input type="radio" name="edit" ref={ref => (radioRef.current[index] = ref)} onChange={() => toggle(index)} />
              </label>
            </>
          )}
        </div>
      </td>
    </tr>
    </>
  );
}

export default EachData;


array.map((data, index)=>{
  return(
    <EachData data={data} index={index} open={false}/>
  )
})

You should use useRef instead of id, here I make some logic with useRef I hope this would be helpful.

As per my understand, when you click on radio button in map function this will be activated and when you click another radio button previous radio button is still showing active, In this code I create two useRef as you see the below code, one is taking for all indexes and second for removing previous radio button active. I hope you understand this code, if you know DOM.

function EachData({ data, index, open }) {
  const [isOpen, setisOpen] = useState(open);

  const radioRef = useRef([]);
  const previousRadioRef = useRef([]);

  const toggle = (i) => {
    setisOpen((prev) => !prev);
    if (previousRadioRef.current && previousRadioRef.current[0] !== radioRef.current[i]) {
      if (radioRef.current[i]) {
        if (previousRadioRef.current.length) {
        previousRadioRef.current[0].checked = false;
        previousRadioRef.current = [];
      }
      radioRef.current[i].checked = true;
      previousRadioRef.current.push(radioRef.current[i]);
    }
    } else if(previousRadioRef.current && previousRadioRef.current[0]) {
       previousRadioRef.current[0].checked = false;
       previousRadioRef.current = [];
   }
  };

  return (
    <>
      <tr>
      <td>
        <div className="functions">
          {!isOpen ? (
            <>
              <label className="far fa-edit">
                <input type="radio" name="edit" ref={ref => (radioRef.current[index] = ref)} onChange={() => toggle(index)} />
              </label>
              <label className="far fa-trash"></label>
            </>
          ) : (
            <>
              <label className="far fa-circle-check"></label>
              <label className="far fa-times-circle">
                <input type="radio" name="edit" ref={ref => (radioRef.current[index] = ref)} onChange={() => toggle(index)} />
              </label>
            </>
          )}
        </div>
      </td>
    </tr>
    </>
  );
}

export default EachData;


array.map((data, index)=>{
  return(
    <EachData data={data} index={index} open={false}/>
  )
})
坏尐絯℡ 2025-02-19 05:47:24

您可以用无线电输入的名称设置状态。
参考: http://react.tips/radio-buttons/radio-buttons-in-radio-buttons-in-reactjs/

You can set state with the name of radio input.
Reference: http://react.tips/radio-buttons-in-reactjs/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文