// Edit3 Problem rozwiązany
Cześć, wie ktoś może czemu moja ścieżka do zdjęcia nie działa ?
Mam błąd Error: Cannot find module '../../common/Product/images/buty1.jpg'
Tak wygląda kod Img <img src={require(
${adress})} alt={singleProduct[0].name} />
Pod zmienną adress kryje się dokładnie to ../../common/Product/images/buty1.jpg
Poprawność tej ścieżki sprawdzałem tak z 15 razy :) Więc gdzie indziej tkwi błąd.
Dodam, że w innym komponencie działa mi tą metodą require
<img src={require(
${props.singleProduct.image})} alt={props.singleProduct.name}/>
A tu cały kod komponentu
import React from 'react';
import products from '../../../stateProducts.js';
class SingleProduct extends React.Component {
constructor( props ){
super( props );
this.state = products;
}
getSingleProduct = () => {
let singleProduct = this.state.products.filter(singleProduct => singleProduct.name == this.props.match.params.id).slice(0, 1);
console.log(singleProduct)
return singleProduct
}
render(){
let imagesFolder = '../../common/Product';
let singleProduct = this.getSingleProduct();
let adress = imagesFolder + singleProduct[0].image.slice(1);
return (
<div className="single-product">
<h1>{this.props.match.params.id}</h1>
<img src={require(`${adress}`)} alt={singleProduct[0].name} />
</div>
)
}
}
export default SingleProduct;
// Edit Tak działa
<img src={require(`${'../../common/Product/images/dres1.jpg'}`)} alt={singleProduct[0].name} />
A tak już nie hmm
let imagesFolder = '../../common/Product/images/dres1.jpg';
<img src={require(`${imagesFolder}`)} alt={singleProduct[0].name} />
// Edit2 Dobra udało się. Dla potomnych, problem tkwił w tym, że użyłem dla tej apki create-react-app i w takim wypadku zdjęcia trzeba trzymać w specjalnym folderze do tego "Public"