简体   繁体   English

如何在 React 中使用 useState 在对象数组中添加 object?

[英]How to add an object in an array of objects using useState in React?

I am working on a page that contains a test with questions, answers and points/grades.我正在处理一个包含问题、答案和分数/等级的测试的页面。 Based on the number of questions selected by admin, it will generate a dynamic inputs fields corresponding to the question, answer and point, with an add question button.根据管理员选择的问题数量,它会生成一个与问题、答案和分数对应的动态输入字段,并带有一个添加问题按钮。 When I press the add question there should be a callback to the handleQuestion function where an object is created with question, answer and points properties.当我按下添加问题时,应该有一个对 handleQuestion function 的回调,其中 object 使用问题、答案和点属性创建。 The values for question, answer and points are set using useState.问题、答案和分数的值是使用 useState 设置的。 I try to add the new object in testContent array using spread operator (...), but when I press "add" after I completed the fields for a question, nothing changes.我尝试使用扩展运算符 (...) 在 testContent 数组中添加新的 object,但是当我在完成问题字段后按“添加”时,没有任何变化。 Can somebody give me an idea on what I am doing wrong?有人可以告诉我我做错了什么吗?

This is the handleQuestion func:这是 handleQuestion 函数:

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

This is where the question, answer and points are setted:这是设置问题、答案和分数的地方:

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>
    ))

And:和:

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

content is not a key on createQuestion, so you will only add undefined to the testContent. content 不是 createQuestion 的键,因此您只会将 undefined 添加到 testContent。 createQuestion only has "questionContent", "questionAnswer" and "questionPoints" as keys createQuestion 只有“questionContent”、“questionAnswer”和“questionPoints”作为键

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

prob you want to do你想做的问题

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

instead反而

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 useState object 数组在 React 中添加 HTML 元素 onClick? - How to add HTML element onClick in React with a useState object array? 如何使用 react useState 钩子切换对象数组 - how to toggle an array of objects using react useState hook 如何使用 useState 反应钩子创建嵌套的对象数组 - How to create nested array of objects using useState react hook 如何使用 map() 或 foreach() 将新对象添加到现有的 useState 数组? - How to add new objects to an existing useState array using map() or foreach()? 用对象数组反应 useState - React useState with Array of objects 如何使用反应钩子 useState 从 API 向对象添加属性 - How to add property to an object from an API using react hooks useState 如何在React JS中的对象数组中添加对象 - How to add an object in the array of objects in react js 如何使用 useState 挂钩更新现有的对象数组(将新的 object 添加到数组中)? - How to update an existing array of objects (add a new object to the array) with the useState hook? 如何使用 React hook 的 useState 将具有嵌套对象的对象数组存储为属性? - How do I store an array of objects with nested objects as attributes using react hook's useState? 将 JSON 响应对象添加到 React Native 中的 useState 数组/字典 - Add JSON response objects to useState array/dictionary in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM