单击CLOSS BTN时,如何清空数组的输入字段?

发布于 2025-01-18 04:21:34 字数 2850 浏览 0 评论 0 原文

我有一个弹出窗口,可以在其中添加属性无限。问题是当我单击“关闭”按钮时,数据阵列正在更新,但在输入字段中它并没有空置。我是一个新手可以做出反应。请我找到解决方案。

import React from "react";
import { useState } from "react";
import { useEffect } from "react";

export default function Property(props) {
  const [Data, setData] = useState([{}]);
  const { setProperties } = props;
  useEffect(() => {
    setData(Data)
  },[Data])
  console.log(Data)
  return (
    <div className='PinputBody'>
      <p className='P-body-Heading-text'>
        Properties show up underneath your item, are clickable, and can be
        filtered in your collection's sidebar.
      </p>
      <form className='Property-Input-Area'>
        <div className='property-grid property-input-header'>
          <p className='property-input-header-text'>Type</p>
          <p className='property-input-header-text'>Name</p>
        </div>
        {Data.map((items, index) => (
          <div key={index} className="property-grid property-input">
            <p className="grid-input grid-flex bdl">
              <div className="delete-input">
                <i className="bx bx-x DeleteClose"
                  onClick={() => {
                    let data = Data;
                    data.splice(index, 1);
                    setData(data);
                    props.setData(data)
                    // setName('');
                    // setType('');
                  }}
                ></i>
              </div>
              <input type="text" className="input-delete-field" placeholder="Enter the prop type"
                value={items?.name}
                onChange={(e) => {
                  let data = Data;
                  data[index].name = e.target.value;
                  setType(e.target.value);
                  setData(data);
                  props.setData(data)
                }} />
            </p>
            <p className="grid-input bdr">
              <input type="text" placeholder="Enter the prop name"
                value={items?.value}
              onChange={(e) => {
                let data = Data;
                data[index].value = e.target.value;
                setName(e.target.value);
                setData(data);
                props.setData(data)
              }} />
            </p>
          </div>
        ))}
        <button type="button" className='Add-more-Btn' onClick={() => { setData([...Data, {}]) }}>
          Add More
        </button>
      </form>
    </div>
  );
}

预先感谢。

I have a popUp where I can add properties unlimited.But then If I want to edit or delete particular row of input fields, I was unable to.Here it is what I have done. The thing is when I am clicking on close button, the Data array is getting updated but it in input fields it is not getting empty.I am a newbie to react.Help me to find the solution.

popUp

import React from "react";
import { useState } from "react";
import { useEffect } from "react";

export default function Property(props) {
  const [Data, setData] = useState([{}]);
  const { setProperties } = props;
  useEffect(() => {
    setData(Data)
  },[Data])
  console.log(Data)
  return (
    <div className='PinputBody'>
      <p className='P-body-Heading-text'>
        Properties show up underneath your item, are clickable, and can be
        filtered in your collection's sidebar.
      </p>
      <form className='Property-Input-Area'>
        <div className='property-grid property-input-header'>
          <p className='property-input-header-text'>Type</p>
          <p className='property-input-header-text'>Name</p>
        </div>
        {Data.map((items, index) => (
          <div key={index} className="property-grid property-input">
            <p className="grid-input grid-flex bdl">
              <div className="delete-input">
                <i className="bx bx-x DeleteClose"
                  onClick={() => {
                    let data = Data;
                    data.splice(index, 1);
                    setData(data);
                    props.setData(data)
                    // setName('');
                    // setType('');
                  }}
                ></i>
              </div>
              <input type="text" className="input-delete-field" placeholder="Enter the prop type"
                value={items?.name}
                onChange={(e) => {
                  let data = Data;
                  data[index].name = e.target.value;
                  setType(e.target.value);
                  setData(data);
                  props.setData(data)
                }} />
            </p>
            <p className="grid-input bdr">
              <input type="text" placeholder="Enter the prop name"
                value={items?.value}
              onChange={(e) => {
                let data = Data;
                data[index].value = e.target.value;
                setName(e.target.value);
                setData(data);
                props.setData(data)
              }} />
            </p>
          </div>
        ))}
        <button type="button" className='Add-more-Btn' onClick={() => { setData([...Data, {}]) }}>
          Add More
        </button>
      </form>
    </div>
  );
}

Thanks in advance.

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

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

发布评论

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

评论(1

自我难过 2025-01-25 04:21:34

这里的问题是您正在使用 data.splice(index, 1); 但您没有为其重新分配一个新数组

修复可能是

data = data.splice(index, 1);

如果您不想删除它但空值,你可以这样做

let data = Data;
data[index] = {name: "", value: ""} //empty your value
const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
setData(updatedData); //assign your new object to the array
props.setData(updatedData)

与你的编辑字段类似的问题

你也需要为编辑的数据分配一个新对象

data[index] = {name: data[index].name, value: e.target.value}
const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
setData(updatedData); //assign your new object to the array
props.setData(updatedData)
data[index] = {name: e.target.value, value: data[index].value}
const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
setData(updatedData); //assign your new object to the array
props.setData(updatedData)

完整的代码可以

import React from "react";
import { useState } from "react";
import { useEffect } from "react";

export default function Property(props) {
  const [Data, setData] = useState([{}]);
  const { setProperties } = props;
  useEffect(() => {
    setData(Data)
  },[Data])
  console.log(Data)
  return (
    <div className='PinputBody'>
      <p className='P-body-Heading-text'>
        Properties show up underneath your item, are clickable, and can be
        filtered in your collection's sidebar.
      </p>
      <form className='Property-Input-Area'>
        <div className='property-grid property-input-header'>
          <p className='property-input-header-text'>Type</p>
          <p className='property-input-header-text'>Name</p>
        </div>
        {Data.map((items, index) => (
          <div key={index} className="property-grid property-input">
            <p className="grid-input grid-flex bdl">
              <div className="delete-input">
                <i className="bx bx-x DeleteClose"
                  onClick={() => {
                    let data = Data;
                    data[index] = {name: "", value: ""} //empty your value
                    const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
                    setData(updatedData); //assign your new object to the array
                    props.setData(updatedData)
                  }}
                ></i>
              </div>
              <input type="text" className="input-delete-field" placeholder="Enter the prop type"
                value={items?.name}
                onChange={(e) => {
                  let data = Data;
                  data[index] = {name: data[index].name, value: e.target.value}
                  const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
                    setData(updatedData); //assign your new object to the array
                    props.setData(updatedData)
                }} />
            </p>
            <p className="grid-input bdr">
              <input type="text" placeholder="Enter the prop name"
                value={items?.value}
              onChange={(e) => {
                let data = Data;
                data[index] = {name: e.target.value, value: data[index].value}
                const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
                    setData(updatedData); //assign your new object to the array
                    props.setData(updatedData)
              }} />
            </p>
          </div>
        ))}
        <button type="button" className='Add-more-Btn' onClick={() => { setData([...Data, {}]) }}>
          Add More
        </button>
      </form>
    </div>
  );
}

The problem here is that you're using data.splice(index, 1); but you don't reassign a new array to it

The fix could be

data = data.splice(index, 1);

If you don't want to delete it but empty values, you can do it this way

let data = Data;
data[index] = {name: "", value: ""} //empty your value
const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
setData(updatedData); //assign your new object to the array
props.setData(updatedData)

Similar problem to your edit fields

You need to assign a new object to the edited data too

data[index] = {name: data[index].name, value: e.target.value}
const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
setData(updatedData); //assign your new object to the array
props.setData(updatedData)
data[index] = {name: e.target.value, value: data[index].value}
const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
setData(updatedData); //assign your new object to the array
props.setData(updatedData)

Full code can be

import React from "react";
import { useState } from "react";
import { useEffect } from "react";

export default function Property(props) {
  const [Data, setData] = useState([{}]);
  const { setProperties } = props;
  useEffect(() => {
    setData(Data)
  },[Data])
  console.log(Data)
  return (
    <div className='PinputBody'>
      <p className='P-body-Heading-text'>
        Properties show up underneath your item, are clickable, and can be
        filtered in your collection's sidebar.
      </p>
      <form className='Property-Input-Area'>
        <div className='property-grid property-input-header'>
          <p className='property-input-header-text'>Type</p>
          <p className='property-input-header-text'>Name</p>
        </div>
        {Data.map((items, index) => (
          <div key={index} className="property-grid property-input">
            <p className="grid-input grid-flex bdl">
              <div className="delete-input">
                <i className="bx bx-x DeleteClose"
                  onClick={() => {
                    let data = Data;
                    data[index] = {name: "", value: ""} //empty your value
                    const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
                    setData(updatedData); //assign your new object to the array
                    props.setData(updatedData)
                  }}
                ></i>
              </div>
              <input type="text" className="input-delete-field" placeholder="Enter the prop type"
                value={items?.name}
                onChange={(e) => {
                  let data = Data;
                  data[index] = {name: data[index].name, value: e.target.value}
                  const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
                    setData(updatedData); //assign your new object to the array
                    props.setData(updatedData)
                }} />
            </p>
            <p className="grid-input bdr">
              <input type="text" placeholder="Enter the prop name"
                value={items?.value}
              onChange={(e) => {
                let data = Data;
                data[index] = {name: e.target.value, value: data[index].value}
                const updatedData = data.map((item,currentIndex) => currentIndex === index ? data[index]: item)
                    setData(updatedData); //assign your new object to the array
                    props.setData(updatedData)
              }} />
            </p>
          </div>
        ))}
        <button type="button" className='Add-more-Btn' onClick={() => { setData([...Data, {}]) }}>
          Add More
        </button>
      </form>
    </div>
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文