mardi 2 juin 2020

I got this error => TypeError: Cannot read property 'image' of undefined

I am very new in this area and I searched in many places and could not find the solution. I get the error "TypeError: Cannot read property 'image' of undefined". I don't know where is the problem.The image of the error is here.

enter image description here

The class with the error is here.

import React, {useEffect} from 'react';
import { Link } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux';
import { detailsProduct } from '../actions/productActions';
function ProductScreen(props){

const productDetails = useSelector(state => state.productDetails);
const {product,loading,error} = productDetails;
const dispatch = useDispatch();

useEffect(() => {
    dispatch(detailsProduct(props.match.params.id));
    return () => {
        //
    }
}, [])

return <div>
    <div className="back-to-result">
        <Link to="/">Back to result</Link>
    </div>
    {loading?<div>Loading...</div>:
    error? <div>{error}</div>:
    (
    <div className="details">
        <div className="details-image">
            <img src={product.image} alt="product"></img>
        </div>
        <div className="details-info">
            <ul>
                <li>
                    <h4>{product.name}</h4>
                </li>
                <li>
                    {product.rating} Stars ({product.numReviews} Reviews)
                </li>
                <li>
                    Price: <b>${product.price}</b>
                </li>
                <li>
                    Description:
                    <div>
                        {product.description}
                    </div>
                </li>
            </ul>
        </div>
        <div className="details-action">
            <ul>
                <li>
                    Price: {product.price}
                </li>
                <li>
                    Status: {product.status}
                </li>
                <li>
                    Qty: <select>
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                    </select>
                </li>
                <li>
                    <button className="button">Add to Cart</button>
                </li>
            </ul>
        </div>
    </div>
    )
    }

</div>
}
export default ProductScreen;

console.log(productDetails) is here

enter image description here




Aucun commentaire:

Enregistrer un commentaire