React 如何更改数组中的数组

石基文
            const Add_Setting = ()=>{ 
            
                const [list, setList] = useState({
                      time : 1,
                      arr1 : [
                         [ {title : 'title1', number : 1},  {title : 'title2', number : 1}],
                         [ {title : 'title1', number : 1},  {title : 'title2', number : 1}]
                      ]
                });
    
    
       const List2 =(props)=>{
           let arr2= list.arr[props.index]["arr2"];
           let list2 = arr2.map((e, i)=>(

        // i want change title and number

                <input type="text" value={e.title}>
                <input type="number" value={e.number}>
           ))
           return list2;
         }
        
        const List = ()=>{
        let list = list.arr.map((e, i)=>(
          <div>
          <List2 index={i}/>
        </div>
        ))
        return list;
        }
        
             return(
                 <div>
                     <List />
                 </div>
              )
          }

你好兄弟!

我想改变数组中的数组,

但是,输入被刷新,只输入一个字符

有办法吗?

天才朋友

如果可能,请让我知道无错误代码!

克里希纳·瓦姆西

我希望这就是你要找的

const Add_Setting = () => {
  const [list, setList] = useState({
    time: 1,
    arr: [
      [{ title: 'title1', number: 1 }, { title: 'title2', number: 1 }],
      [{ title: 'title1', number: 1 }, { title: 'title2', number: 1 }],
    ],
  });

  const onChange = (e, primaryIndex, secondaryIndex) => {
    setList((oldList) => {
      const newArr = [...oldList.arr];
      newArr[primaryIndex][secondaryIndex].number = e.target.value;
      return {
        ...oldList,
        arr: newArr,
      };
    });
  };

  const List2 = ({ index: primaryIndex }) => list.arr[primaryIndex].map((e, secondaryIndex) => (
    <>
      <input type="text" value={e.title} />
      <input type="number" value={e.number} onChange={(ev) => onChange.bind(null, ev, primaryIndex, secondaryIndex)} />
    </>
  ));

  const List = () => list.arr.map((_, i) => (
    <div>
      <List2 index={i} />
    </div>
  ));

  return (
    <div>
      <List />
    </div>
  );
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用 React 和 useEffect 更改数组中的位置?

如何在React中更改数组元素的状态

如何更改数组javascript中对象的属性(React Native)

调用动作时,如何在React Redux中更改数组中对象的值?

React Hook如何更改数组对象的属性

如何在React Native中使用索引更改数组中的值

使用react hook setState更改数组中的变量

在 React 中更改数组项的文本:undefined 不是函数

如何通过在 expo/react-native 中的 JavaScript 中找到与另一个数组中的 id 相似的 id 来更改数组中的对象?

如何在React上修改数组

在 React 组件的状态中修改数组

如何更改React数组中的行?

在React Native中使用onComplete事件更改数组中的图像

尝试使用 react 在单击时更改数组中的 img src

使用状态 React Native 更改数组的值

React - 更改数组的状态无法正确呈现

React useState如何修改数组中的第一个对象

如何基于React / JS中的另一个数组修改数组的每个对象的属性?

React / JavaScript添加到数组不会更改数组长度

javascript:React-向对象添加数组会更改数组的格式

在react js中对其进行更改后如何映射数组?

如何更改数组中的值排列?

如何更改数组中的值?

如何更改数组中的位置?

如何更改数组中对象的顺序

如何更改数组中的结果?

如何分别更改数组中的颜色?

如何更新/更改数组中的对象?

如何从React组件访问数组中的数组?