gpt4 book ai didi

arrays - 为什么我的数组在 React 中接收它的函数处为空?

转载 作者:行者123 更新时间:2023-12-05 05:11:08 26 4
gpt4 key购买 nike

我从提取到我的 python API 中获得了数据。使用数据,我创建了一个新的 json 字符串数组。如果我执行 console.log(array),我可以看到该数组包含 2 个条目。当我将该数组传递给子元素的函数时,该数组为空。我做错了什么?

这是我的代码。我对返回数据的 python api 的调用:

        var myArray = [];
const baseUrl = 'http://localhost:5000';
const apiUrl = baseUrl + '/api/load_data';
fetch(apiUrl, {
method: 'GET',
headers: {
'X-Api-Key': 'react_main',
'Access-Control-Allow-Origin': 'http://localhost:8080/',
'Access-Control-Allow-Headers': 'Content-type, Authorization',
'Accept': 'application/json',
'Content-type': 'application/json',
},
})
.then(res => res.json())
.then( res => {
res.map(function(dataObj) {
const data = {
id: dataObj['id'],
imgLocation: {backgroundImage: dataObj['image']},
city: dataObj['city'],
state: dataObj['state']
}
myArray.push(data);
}
)

console.log(myArray);
});

然后我将它进一步传递给下面的函数:

    import PropTypes from "prop-types";
import React from "react";

class HomePage extends React.Component {
constructor(props) {
super(props);
this.state = {
cards: [],
}
}

componentDidMount(){
const baseUrl = 'http://localhost:5000';
const apiUrl = baseUrl + '/api/load';

fetch(apiUrl, {
method: 'GET',
headers: {
'X-Api-Key': 'react_main',
'Access-Control-Allow-Origin': 'http://localhost:8080/',
'Access-Control-Allow-Headers': 'Content-type, Authorization',
'Accept': 'application/json',
'Content-type': 'application/json',
},
})
.then(res => res.json())
.then( res => {
res.map(function(cardObj) {
const card = {
title: cardObj['title'],
imgLocation: {backgroundImage: cardObj['image']},
city: cardObj['city'],
state: cardObj['state'],
url: cardObj['url'],
}
this.setState(prevState => ({cards: [...prevState.cards, card]}))
}
)
}).catch((e) => console.log(`Error! ${e.message}`));
}

render() {
var divStyle = {
clear: 'both',
display: 'flex',
justifyContent: 'center'
};

return(
<div>
<MainComponents.HeaderElement />
<div className="gray-bg-wrapper">
<section>
<h1 className="section__title">New Cards</h1>
<h2 className="section__subtitle">Most recent Cards added</h2>
<div className="cards">
<MainComponents.CardElement listCards={this.state.cards}
/>
</div>

<div style={divStyle}>
<a href="/browse/" className="btn btn--cta cards__more-btn">Browse Cards</a>
</div>
</section>
</div>
<MainComponents.FooterElement />
</div>
)
}
}

在 MainComponents.CardElement 函数中。 listData 为空:

    export var CardElement = function(props) {
return(
props.listCards.map(function (item) {
return (
<div className="card" key={item.title.replace(' ', '_')}>
<div className="exp-card">
<a className="nostyle" target="_blank" href={item.url}>
<div className="exp-card-image" style={item.imgLocation}>
<div className="card-fader"></div>
</div>
<div className="exp-card-info">
<div className="exp-card-detail">
<div className="exp-card-title">{item.title}
</div>
<div className="exp-card-location">
<i className="fa fa-map-marker"></i>
<span className="neighborhood">{item.city}, {item.state}</span>
</div>
</div>
</div>
</a>
</div>
</div>
)
})
)
}
CardElement.propTypes = {
listCards: PropTypes.array.isRequired,
}

最佳答案

我想通了。将我的 componentDidMount() 函数更改为以下使其工作。感谢所有注意到我没有设置状态的人。

    componentDidMount(){
const baseUrl = 'http://localhost:5000';
const apiUrl = baseUrl + '/api/load';
let currentComponent = this;

fetch(apiUrl, {
method: 'GET',
headers: {
'X-Api-Key': 'react_main',
'Access-Control-Allow-Origin': 'http://localhost:8080/',
'Access-Control-Allow-Headers': 'Content-type, Authorization',
'Accept': 'application/json',
'Content-type': 'application/json',
},
})
.then(res => res.json())
.then( res => {
let arrCards = [];
res.map(function(cardObj) {
const card = {
title: cardObj['title'],
imgLocation: {backgroundImage: cardObj['image']},
url: cardObj['url'],
city: cardObj['city'],
state: cardObj['state'],
}
arrCards.push(card);
currentComponent.setState(function(){
return {
cards: arrCards
}
})
}
)
}).catch((e) => console.log(`Error! ${e.message}`));
}

关于arrays - 为什么我的数组在 React 中接收它的函数处为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55802458/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com