【发布时间】:2022-08-15 12:29:35
【问题描述】:
我有一个包含另一个数组的状态。我需要获取此数组以将其作为列表返回。新项目应作为对象出现在应用程序数组中。我不太明白我做错了什么。我怎样才能解决这个问题?
** enter image description here
import React, { useState } from \'react\'
function App() {
const [data, setData] = useState([
{
name: \'Ivan Pupkin\',
email: \'ivan@gmail.com\',
phone: \'+34452344323\',
application: [
{
nameOfApp: \'Name of App\',
type: \'It and business\',
description: \'some description\',
},
],
},
])
const [name, setName] = useState(\'\');
const [type, setType] = useState(\'\');
const [description, setDescription] = useState(\'\');
const addNewUser = (e) => {
e.preventDefault()
setData(current => current.map(item => [...item.application, {
personalId: 4,
nameOfApp: name,
description: description,
type: type
}]))
}
const Users = data.map(item => item.application.map((elem, index) => {
return(
<div key={index}>
<div>{elem.nameOfApp}</div>
<div>{elem.type}</div>
<div>{elem.description}</div>
</div>
)
}))
return (
<div>
<form action=\"#\">
<input onChange={(e) => setName(e.target.value)} placeholder=\'name\'/>
<input onChange={(e) => setType(e.target.value)} placeholder=\'type\'/>
<input onChange={(e) => setDescription(e.target.value)} placeholder=\'desc\'/>
<button onClick={addNewUser} type=\'submit\'>submit</button>
</form>
<br />
<br />
<br />
{Users}
</div>
)
}
export default App
-
欢迎来到堆栈溢出!你的问题不清楚。请详细说明您要问什么。要了解有关此社区的更多信息以及我们如何为您提供帮助,请从 tour 开始并阅读 How to Ask 及其链接资源。
-
怎么改?要阅读它,您只需使用
data或打印它
标签: javascript reactjs