【问题标题】:onChangeHandler :: how to manipulate the same input formonChangeHandler :: 如何操作相同的输入表单
【发布时间】:2022-01-13 18:10:14
【问题描述】:

我正在使用 React 实现“重复的英文单词形式”,但我不知道如何处理在 onChangeInputHandler 变量中包含多个对象的 wordList

WordListonClickCardAddHandler 一起单击时,对象inputData 将被添加,我想使用从@ 的onChange 接收到的索引与onChange 分开调整输入表单987654328@组件。

这是我的代码:

// AddWords.jsx(Parent component)

export default function AddWords() {
  const [wordList, setWordList] = useState([
    {
      spelling: "wordlist",
      meaning: "",
      category: "",
    },
  ]);
 

  const { spelling, meaning, category } = wordList;

  const inputData = {
    spelling: "",
    meaning: "",
    category: "",
  };

  const onChangeInputHandler = (e, index) => {
    const { value, name } = e.target;
    setWordList({
      ...wordList,
      [name]: value,
    });
  };

  const onClickCardAddHandler = () => {
    let WordInputs = [...wordList];
    let nextWordInputs = inputData;
    WordInputs.push(nextWordInputs);
    setWordList(WordInputs);
  };

  return (
    <Container>
      <AddWordsForm
        inputs={inputs}
        wordList={wordList}
        setWordList={setWordList}
        onChangeInputHandler={onChangeInputHandler}
      />
      <CardAddContainer onClick={onClickCardAddHandler}>
        <AddIcon /> add Card
      </CardAddContainer>
    </Container>
  );
}
//AddWordsForm.jsx 

import React, { useState } from "react";
import Input from "@mui/material/Input";
import styled from "styled-components";
import NativeSelect from "@mui/material/NativeSelect";
export default function AddWordsForm({
  wordList,
  onChangeInputHandler,
}) {
  const { spelling, meaning, category } = wordList;
  const categoryList = ["n", "v", "adj", "adv", "phr", "prep"];

  return (
    <>
      {wordList &&
        wordList.map((item, index) => (
          <CardContainer key={index}>
            <div>
              <div>
                <label>단어</label>
                <Input
                  name="spelling"
                  value={spelling}
                  onChange={(e) => onChangeInputHandler(e, index)}
                />
              </div>
              <div>
                <label>뜻</label>
                <Input
                  name="meaning"
                  value={meaning}
                  onChange={(e) => onChangeInputHandler(e, index)}
                />
              </div>
              <div>
                <label>품사</label>
                <NativeSelect
                  name="category"
                  value={category}
                  onChange={(e) => onChangeInputHandler(e, index)}
                >
                  {categoryList.map((category, index) => (
                    <option key={index} value={category}>
                      {category}
                    </option>
                  ))}
                </NativeSelect>
              </div>
            </div>
          </CardContainer>
        ))}
    </>
  );
}
const CardContainer = styled.div`
  background-color: lightgray;
  padding: 2vh;
  margin: 2vh;
  width: 35vh;
  border-radius: 1.5vh;
`;

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    检查您当前的状态,可以为您提供线索

      const onChangeInputHandler = (e, index) => {
        const { value, name } = e.target;
    
        setWordList( wordList.map( (item, i) => {
           if(i === index)
             return {...item, ...{[name]:value}}
           else
             return item
    
        }));
      };
    

    【讨论】:

    • 非常感谢?
    • 不客气,请给我投票或接受的回答状态
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多