I have a weird probleme here, so i am working with redux and this is my component code for a selected product from reducer state.
import './productInfo.css';
import { useParams } from 'react-router-dom';
import { getProduct } from '../../actions/productActions';
import { connect } from 'react-redux';
import { useEffect, useState } from 'react';
const ProductInfo = (props) => {
const [usedProduct, setUsedProduct] = useState(props)
const {id} = useParams();
useEffect(() => {
props.getProduct(id);
}, [usedProduct])
let newUsedProduct = usedProduct;
console.log(usedProduct.products[0].name)
return (
<div>
</div>
);
}
const mapStateToProps = (state) => {
return{
products : state.myProduct.product
}
}
export default connect( mapStateToProps, { getProduct })(ProductInfo);
so when i launch the component for the first time it works and i got the necessary data from the reducer state but when i refresh the page it gives me this error : Uncaught TypeError: Cannot read properties of undefined (reading '0') in this line of code :
console.log(usedProduct.products[0].name)
i'm totally lost !