I've some trouble while using React for my first time. I wrote a simple class as follow in my project :
import React, { Component } from 'react';
import axios from 'axios';
import logo from './logo.svg';
import './App.css';
class FigosTest extends React.Component {
state = {
provider: 'unknown',
date: 'sometime'
};
componentDidMount() {
axios.get('x.com/some_api')
.then(res => {
console.log(res.data.provider);
console.log(res.data.date);
this.setState(this.setState(res.data));
console.log("After");
console.log(this.state.provider);
console.log(this.state.date);
});
}
render() {
return this.provider || 'undefined';
}
}
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Result: <FigosTest/>
</p>
</header>
</div>
);
}
}
export default App;
I want to get and display some data from the API I'm asking. I used to do this a call to this API that will render me an object containing only provider
and date
labels.
Let's say I want to display the provider. I see with console.log
that my component achieve to query the API, and to return my values.
My problem is that the component wouldn't update after this operation.
I tried to callback App.render, to use schouldComponentUpdate, and in others posts, I noticed that everybody use ReactDOM.render()
. What is it ? How to use it ?
Regards.