We have images in folders, named (face1.png,...,face6.png) using this code; I can't display images, we randomly retrieve photos:
import React from "react";
import jeuxde from "../images/jeuxde.jpg";
export default class JeuDe extends React.Component{
constructor (props){
super(props);
this.state= {face: null, compteur: 0, fin: false};
};
jouer(){
const valeur = Math.floor(Math.random()*6)+1;
this.setState({
face:this.state.face = valeur,
compteur:this.state.compteur+=valeur,
})
const image = document.getElementById("des")
image.src = '../images/face' +valeur + '.png'
}
initialiser (){
this.setState({
face: null, compteur: 0, fin: false
});
}
render(){
const styleImage={width:"100px",height:"100px"};
return (
<div>
<img src={jeuxde} alt={"jeux de"}/>
<h1>Jeu de Dé...</h1>
<h2>face:{this.state.face}</h2>
<div >
<img id="des" style={styleImage} />
</div>
<h2>nombre dessais {this.state.compteur}</h2>
<button onClick={(()=>this.jouer())}>jouer</button>
<p>Bravo vous avez trouvez la face cachée...</p>
<button onClick={(()=>this.initialiser())}>Initialiser</button>
</div>
);
}
}
The problem:
when I use inspect, I find the pictures change in the src but not display in DOM,but when I use it I get
<img id="des" style="width: 100px; height: 100px;" src="../images/face3.png">
What I have tried:
I'm using this code but images don't display in the DOM,
const image = document.getElementById("des")
image.src = '../images/face' +valeur + '.png
This <
src="../images/face3.png">is added when the button <
this.jouer())}>jouer > is clicked randomly but images don't display in the DOM.