[英]JSX function in React not returning data?
我正在创建一个简单的 GET API,它收集数据并将其作为基本文本输出到我的前端。 API 工作并在控制台中显示数据,但前端没有返回任何内容。 请帮忙:
import React, { Component } from 'react';
import './Cards.css'
const data = {
name: 'test',
hobby: 'test'
}
function getUsers (data) {
fetch('http://localhost:3001/profile', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
})
.then((response) => response.json())
.then((data) => {
console.log('Success:', data)
})
.then((data) => {
return data.name
})
.catch((error) => {
console.error('Error:', error)
});
};
function Cards (data) {
return (
<div id='cards'>
<div className="card-header" id='card-header'>
Header
</div>
<div className="card-body" id='card-body'>
<blockquote className="blockquote mb-0">
<p>
{getUsers(data)}
</p>
<footer className="blockquote-footer">Someone famous in <cite title="Source Title">Source Title</cite></footer>
</blockquote>
</div>
</div>
);
}
export default Cards;
解决这个问题的正确方法是从 API 获取数据(您可以通过useEffect
钩子触发调用),将结果存储在组件的状态中,然后在 JSX 中使用此状态值。
import React, { useState, useEffect } from "react";
import "./Cards.css";
const data = {
name: "test",
hobby: "test"
};
function Cards(data) {
const [name, setName] = useState("")
useEffect(() => {
getUsers()
}, [getUsers])
function getUsers(data) {
fetch("http://localhost:3001/profile", {
method: "GET",
headers: {
"Content-Type": "application/json"
}
})
.then(response => response.json())
.then(data => {
console.log("Success:", data);
})
.then(data => {
setName(data.name)
return data.name;
})
.catch(error => {
console.error("Error:", error);
});
}
return (
<div id="cards">
<div className="card-header" id="card-header">
Header
</div>
<div className="card-body" id="card-body">
<blockquote className="blockquote mb-0">
<p>{name}</p>
<footer className="blockquote-footer">
Someone famous in <cite title="Source Title">Source Title</cite>
</footer>
</blockquote>
</div>
</div>
);
}
export default Cards;
我认为如果你要链接另一个数据,你应该返回数据。然后,第一个是控制台正确记录数据但第二个没有返回是有道理的。
.then((data) => { console.log('Success:', data) [should be a 'return data' here?] }) .then((data) => { return data.name })
而且你的函数是异步的,所以它应该在 useEffect 回调中。
您可以按照注释中的说明使用useEffect
和 state,下面的代码应该可以工作:
import React, { Component, useEffect } from 'react';
import './Cards.css'
const data = {
name: 'test',
hobby: 'test'
}
function Cards (data) {
const [ users, setUsers] = useEffect('')
useEffect(() => {
function getUsers (data) {
return fetch('http://localhost:3001/profile', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
}).then((response) => response.json())
.catch((error) => {
console.error('Error:', error)
});
};
getUsers(data).then((data) => setUsers(data.name) )
}, [])
return (
<div id='cards'>
<div className="card-header" id='card-header'>
Header
</div>
<div className="card-body" id='card-body'>
<blockquote className="blockquote mb-0">
<p>
{users}
</p>
<footer className="blockquote-footer">Someone famous in <cite title="Source Title">Source Title</cite></footer>
</blockquote>
</div>
</div>
);}
export default Cards;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.