import React, {useState, useEffect} from 'react';
import MakeCard from './MakeCard.js';
export default function GameCards(props) {
const [images, setImages] = useState([{}]);
// Render component after fetching images
useEffect(() => {
getImages(props.cards).then(images => setImages(images));
}, [props.cards]);
// shuffle images and update state.
const shuffleCards = (e) => {
console.log("clicked");
let newImages = images;
for (let i = newImages.length - 1; i >= 0; i--) {
let temp, j;
j = Math.floor(Math.random() * i);
temp = newImages[i];
newImages[i] = images[j];
newImages[j] = temp;
}
setImages(newImages);
console.log("newImages: ", images);
}
if (images.length === 0) {
return (<h1>Loading...</h1>)
} else {
return (<div>
{
images.map((image, i) => <MakeCard image={image} key={i}
// shuffleCards={shuffleCards}
/>)
}
</div>)
}
}
So I am trying to make a Memory_Card_Game in react but when I shuffle the image array(when the user clicks on the image) which is a state variable. React does not render. I read in the documentation that react updates the virtual-DOM tree when there is a change in the state of the component. Why it isn't rendering after change in images array ?
const [images, setImages] = useState([{}]);