我有问题,我不知道怎么解决它. 所以我有一个组件,我在其中声明了一个对象array. 我想单独设置它的状态,但我不想声明多个useState.
我有一个对象数组,如下所示:
const [card, setCard] = useState({
name: "",
questions: [
{
question: "",
answer: "",
},
{
question: "",
answer: "",
},
{
question: "",
answer: "",
},
{
question: "",
answer: "",
},
{
question: "",
answer: "",
},
{
question: "",
answer: "",
},
{
question: "",
answer: "",
},
{
question: "",
answer: "",
},
{
question: "",
answer: "",
},
],
});
下面是它的组成部分:
const NewCard = () => {
const handleNameChange = (event) => {
setCard({ name: event.target.value, ...questions });
};
return (
<div className="newcard-container">
<div className="card-container">
<h3>Podaj nazwe fiszki</h3>
<input type="text" value={card.name} />
</div>
<div className="questions-container">
{card.questions.map((q) => {
return (
<div className="question">
<h4>Podaj pytanie </h4>
<input type="text" value={q.question} />
<h4>Podaj odpowiedź</h4>
<input type="text" value={q.answer} />
</div>
);
})}
<button>Dodaj pytanie</button>
</div>
</div>
);
};
我试图弄清楚如何更改setState以实现这种方法,但我没有成功.你知道我怎么才能弄到吗?