Images not loading on route with params but works on routes without params

Viewed 2086

Hello for some reason I am having a problem where images won't load on routes with params. Yet on any route without params they load just fine.

so for example I load route '/product/:id';

that component for that route contains this

class Product extends Component {
constructor(props) {
    super(props);

    this.state = {
        product: null
    }
}

componentDidMount() {
    this.fetchProduct();
}

fetchProduct() {
    const { id } = this.props.match.params;

    axios.get(`/api/product/${id}`).then(function (response) {
        this.setState({ product: response.data });
    }.bind(this));
}

renderProduct() {
    let { product } = this.state;

    if (!product) {
        return false
    }
    else {
        return (
            <div className='row'>
                <div className='col-md-5'>
                    <img src={product.main_img} />//this image is not displaying
                    <h5>{product.title}</h5>
                    <p>{product.description}</p>
                </div>
                <div className='col-md-5'>
                    <Form product={product}/>
                </div>
            </div>
        )
    }
}

render() {
    return (
        <div className='container'>
            {this.renderProduct()}
        </div>
    )
 }
}

Ok so all the information for that product shows correctly and I can console.log(product.main_img) and that is the correct image. So why isn't the image showing? I have the exact same image on another route without params that shows fine. I cant seem to figure it out.

1 Answers

I had the same problems, I fixed it but changing all my links to images to start with a slash '/' then directory name instead of './' or just the directory name.

Related