I'm new to React and Redux. I would like to ask why if I'm setting an object in a setState when I try to use it or show it in the console is undefined.
import React, { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import { Row, Col } from "react-bootstrap";
import axios from 'axios';
import ProductImage from './Sections/ProductImage';
import ProductInfo from './Sections/ProductInfo';
function DetailProduct (props){
const productId = props.match.params.productId
const [Product, setProduct] = useState()
useEffect(()=>{
axios.get('http://localhost:5000/products/'+productId)
.then(response =>{
setProduct(response.data)
console.log('state '+Product._id)
})
}, [])
return (
<div className="postPage" style={{width: '100%', padding: '3rem 4rem'}}>
<div style={{display:'flex',justifyContent:'center'}}>
<h1>{Product._id}</h1>
</div>
<br/>
<Row gutter={[16, 16]}>
<Col lg={12} xs={24}>
<ProductImage />
</Col>
<Col lg={12} xs={24}>
<ProductInfo />
</Col>
</Row>
</div>
)
}
export default DetailProduct
When the page loads it says that Product is undefined, even though it's suppose to have an object. When I console.log what response.data has it shows me the information that product would need to have. Sorry for bad english and thank you.