英文:
React: I can't print the data fetched
问题
如我上面所写的,我无法打印从fetch中恢复的数据,我已经花了一些时间但没有任何结果。
我是React的新手,正在学习如何使用hooks。
我得到的错误是:
无法读取未定义的属性(读取'map')
import React, { useEffect, useState } from 'react';
import './App.css';
export default function App() {
const [users, setUsers] = useState();
useEffect(async () => {
await fetch('https://jsonplaceholder.typicode.com/users/').then(response => response.json()).then(data => setUsers(data))
}, []);
return (
<div>
{
users.map((user) => {
<div>{user.name}</div>
})
}
</div>
)
}
你能帮助我吗?
英文:
As I wrote above, I can't print the data recovered from the fetch, I've been on this for some time without any results.
I'm new to React and learning how to use hooks.
The error I'm getting is:
Cannot read properties of undefined (reading 'map')
import React, { useEffect, useState } from 'react';
import './App.css';
export default function App() {
const [users, setUsers] = useState();
useEffect(async () => {
await fetch('https://jsonplaceholder.typicode.com/users/').then(response => response.json()).then(data => setUsers(data))
}, []);
return (
<div>
{
users.map((user) => {
<div>{user.name}</div>
})
}
</div>
)
}
Can you kindly help me?
答案1
得分: 3
- 将一个空数组作为默认值添加到
useState
中,以确保map()
始终存在。(第一次渲染将在空数组上进行map
操作,这是可以的,因为还没有数据)const [users, setUsers] = useState([]);
useEffect
的回调函数不应该是await/async
,将其移除。- 你没有从
map()
中返回任何内容,我已经将其更改为:{ users.map((user) => { return <div>{user.name}</div> }) }
或者使用简写的返回方式:
{ users.map(user => <div>{user.name}</div>) }
const { useState, useEffect } = React;
const App = () => {
const [users, setUsers] = useState([]);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/users/')
.then(response => response.json())
.then(data => setUsers(data))
}, []);
return (
<div>
{
users.map((user) => {
return <div>{user.name}</div>
})
}
</div>
)
}
ReactDOM.render(<App />, document.getElementById("react"));
html, body { height: 100vh; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>
英文:
- Add a default value as empty array to
useState
to make sure thatmap()
always exist. (The first render willmap
on an empty array, which is fine since there is not data yet)const [users, setUsers] = useState([]);
useEffect
callback should not be await/async, removed that- You didn't
return
anything from yourmap()
, I've changed it to:{ users.map((user) => { return <div>{user.name}</div> }) }
Or as the short-hand return style:
{ users.map(user => <div>{user.name}</div>) }
- <https://stackoverflow.com/questions/39999671/map-function-not-working-in-react>
- <https://stackoverflow.com/questions/70361452/react-js-map-is-not-rendering-components>
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
const { useState, useEffect } = React;
const App = () => {
const [users, setUsers] = useState([]);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/users/')
.then(response => response.json())
.then(data => setUsers(data))
}, []);
return (
<div>
{
users.map((user) => {
return <div>{user.name}</div>
})
}
</div>
)
}
ReactDOM.render(<App />, document.getElementById("react"));
<!-- language: lang-css -->
html, body { height: 100vh; }
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>
<!-- end snippet -->
答案2
得分: 0
问题在于在开始时users
是undefined
,你有两种方法可以解决这个问题:
- 将一个空数组设置为
users
的默认值:useState([])
const [users, setUsers] = useState([]);
- 当
users
为undefined
时,显示其他内容,比如加载文本
import React, { useEffect, useState } from 'react';
import './App.css';
export default function App() {
const [users, setUsers] = useState();
useEffect(async () => {
await fetch('https://jsonplaceholder.typicode.com/users/').then(response => response.json()).then(data => setUsers(data))
}, []);
return (
<div>
{!users && "loading users..."}
{
users && users.map((user) => {
<div>{user.name}</div>
})
}
</div>
)
}
英文:
The issue is that users
is undefined
at the start, you have 2 ways to solve this:
- set an empty array as default for
users
:useState([])
const [users, setUsers] = useState([]);
- show something else like a loading text when
users
is undefined
import React, { useEffect, useState } from 'react';
import './App.css';
export default function App() {
const [users, setUsers] = useState();
useEffect(async () => {
await fetch('https://jsonplaceholder.typicode.com/users/').then(response => response.json()).then(data => setUsers(data))
}, []);
return (
<div>
{!users && "loading users..."}
{
users && users.map((user) => {
<div>{user.name}</div>
})
}
</div>
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论