I have this quote.jsx file with a React component:
import React from 'react';
import fetch from 'node-fetch';
import './quotes.css';
export default class Quotes extends React.Component {
constructor(props) {
super(props);
}
render() {
return(
<div className="quotes">
{
fetch('/random_quote')
.then(res => res.json())
.then(json => json.text())
}
</div>
);
}
}
As you may see, it uses fetch to get a JSON object with data related to a quote (quote text and author) and display some this data.
When I use webpack to bundle my system, I'm getting this warning message:
WARNING in ./fe/~/encoding/lib/iconv-loader.js 9:12-34 Critical dependency: the request of a dependency is an expression
And when I run my web system and visit the URL who should display this component, I got the following error:
Uncaught Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of
Quotes.
There is no error in my back-end and the JSON is being correctly generated and returned. This is not an issue.
I am relatively new to React.js and had never seen this message before.
Can someone help me with this? I've been fighting this error all my Sunday and have no more options to deal with it.
componentDidMountthen save the result into state, and inrenderaccess the state.