我的用户数据包括一个ID、用户名和一个密码。
我正在尝试将我拥有的数据作为对象数组发送。它使用 .map 注释一一发送,但我想将其作为整个数组发送。这可能吗?来自我的数据库的 json 格式。我想将我的对象作为数组发送到用户表文件。它变得不确定。如何将对象数组作为 props 发送?
function User() {
const [userList,setUserList] = useState([])
const [error,setError] = useState(null);
const getAllUsers = async() =>{
const a = await axios.get(BASE_URL + "/users/all")
setUserList(a.data);
}
useEffect(()=>{
getAllUsers()
.then(()=>{
console.log("Users Promise")
})
return(
<div>
{<UserTable data = {userList} />}
</div>
)
}
UserTable.jsx
function UserTable({props}) {
debugger
const {id,userName,password} = props; // It comes undefined here.
console.log(props)
}
您正在寻找一个名为的道具
props
:但是你正在传递一个名为
data
:名称需要匹配。例如:
另外...您传递的道具是一个数组:
数组没有称为
id
oruserName
或 的属性password
。如果该数组中的各个对象具有这些属性,那么您将希望使用这些单独的对象。问题中并未真正说明您如何使用它们。但总的来说,重点是对象数组与对象不同,就像一篮子苹果不是苹果一样。
例如,如果您想显示这些值,您可以
.map()
覆盖它们: