I'm trying to get data from an API and put it into my state before rendering it on the component.
However, the empty state is keep being called before any value is being put into it.
Here's the code :
const cookies = new Cookies();
class ProductDetails extends Component {
state = {product:null}
componentWillMount(){
this.getSelectedProduct()
}
getSelectedProduct(){
var selected = cookies.get('SelectedProduct')
Axios.get('http://localhost:1002/products/' + selected)
.then(ok=>{
console.log(ok.data)
this.setState({product:ok.data})
console.log(this.state.product[0].ProductName)
})
}
renderContent(){
console.log(this.state.product)
if(this.state.product !== [] ){
const {ProductName, ProductPrice, Description, Brand, Color, ScreenSize, RAM, Storage, Stock} = this.state.product[0]
return([
ProductName,
ProductPrice,
Description,
Brand,
Color,
ScreenSize,
RAM,
Storage,
Stock
])
}
}
render(){
return(
<div >
<h3>{this.renderContent().ProductName}</h3>
</div>
)
}
}
export default ProductDetails