【问题标题】:How to add an object in an array of objects using useState in React?如何在 React 中使用 useState 在对象数组中添加一个对象?
【发布时间】:2021-06-09 19:55:42
【问题描述】:

我正在处理一个包含问题、答案和分数/等级的测试的页面。根据管理员选择的问题数量,它会生成一个与问题、答案和分数对应的动态输入字段,并带有一个添加问题按钮。当我按下添加问题时,应该有一个对 handleQuestion 函数的回调,在该函数中创建了一个带有 question、answer 和 points 属性的对象。 问题、答案和分数的值是使用 useState 设置的。我尝试使用扩展运算符 (...) 在 testContent 数组中添加新对象,但是当我在完成问题字段后按“添加”时,没有任何变化。有人可以告诉我我做错了什么吗?

这是句柄问题函数:

const handleQuestion = (e) => {
    e.preventDefault();
    const createQuestion= {
       "questionContent": question,
       "questionAnswer": answer,
       "questionPoints": points 
    }
    setTestContent(testContent => [...testContent, createQuestion]);
}

这是设置问题、答案和分数的地方:

return  questions.map((quest) => (
    <div>
        <div>
            <label htmlFor="testContent"> Question {quest + 1} </label>
            <input
                type="text"
                className="form-control"
                name="question"
                //value={question}
                onChange={onChangeQuestion}
                validations={[required, validContent]}
            />
        </div>
        <div>
            <label htmlFor="testContent"> Answer for question {quest + 1}</label>
            <input
                type="text"
                className="form-control"
                name="answer"
                //value={question}
                onChange={onChangeAnswer}
                validations={[required, validContent]}
            />
        </div>
        <div>
            <label htmlFor="testContent"> Points for question {quest + 1}</label>
            <input
                type="text"
                className="form-control"
                name="points"
                // value={}
                onChange={onChangePoints}
                validations={[required, validContent]}
            />
        </div>
        <button onClick={handleQuestion}>Add question</button>
    </div>
    ))

还有:

const[question, setQuestion] = useState("");
const[answer, setAnswer] = useState("");
const[points, setPoints] = useState("");
const[testContent, setTestContent] = useState([]);

【问题讨论】:

    标签: javascript json reactjs react-hooks


    【解决方案1】:

    content 不是 createQuestion 的键,因此您只需将 undefined 添加到 testContent。 createQuestion 只有“questionContent”、“questionAnswer”和“questionPoints”作为键

    const createQuestion= {
       "questionContent": question,
       "questionAnswer": answer,
       "questionPoints": points 
    }
    

    你想做的事情

    setTestContent(testContent => [...testContent, createQuestion]); 
    

    改为

    【讨论】:

    • 我想在 testContent 数组中添加整个 createQuestion 对象
    • 然后你可以做 setTestContent(testContent => [...testContent, createQuestion]);
    • 我试过了,如果我点击一次按钮,它不会做任何改变,如果我点击两次,它就会把我的对象放在那个数组中。但是例如,当我完成第二个问题的字段,然后按添加时,它会再次添加上一个对象,第二次按添加时会添加第二个对象。
    猜你喜欢
    • 2021-06-23
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 2021-11-12
    • 1970-01-01
    • 2022-01-23
    • 2021-02-13
    相关资源
    最近更新 更多