I'm trying to render a simple return value from an async function in my react app however every time I try to my entire app will not render at all. Without calling the function my app renders fine. Also if I console.log my return value (result) in my function it returns the correct value on the console but nothing in the app renders. Any ideas what's going on?
class TitleCards extends Component {
constructor(props){
super(props)
this.totalPortfolio = this.totalPortfolio.bind(this);
this.getIntradayPrice = this.getIntradayPrice.bind(this);
}
async getIntradayPrice(tick) {
const resp = await fetch(`${IEX.base_url}/stock/${tick}/intraday-prices?chartLast=1&token=${IEX.api_token}`);
return resp.json();
}
async totalPortfolio() {
const respPromises = this.props.info.map(({ tick }) => this.getIntradayPrice(tick));
const respArrays = await Promise.all(respPromises);
console.log(respArrays);
const result = respArrays.reduce((acc, val, index) => acc + val[0].close * this.props.info[index].amtPurch, 0)
console.log(result);
return result;
}
render(){
return(
<div className="positioning">
<div className="StockCardTitle">
<img src={Folder} className="StockCardTitle-image" />
{this.totalPortfolio()}
</div>
</div>
)
}
}
export default TitleCards;
rendermethod. Therendermethod is also considered a pure function, meaning it should have zero side effects, like fetching data. What isthis.portfolioin the above snippet? Can you provide a more complete code example so we may see whatportfoliofunction is and what is callinggetIntradayPriceandtotalPortfolio?