It is supposed to execute only the function shuffleCards when I click the button but instead it also executes the two console.log() statements, the one before and after the function.
Why is this happening?
The code:
import { useState } from 'react';
const cardImages = [
{"text": "A"},
{"text": "B"},
{"text": "C"},
{"text": "D"},
{"text": "E"},
{"text": "F"},
]
function App() {
const [cards, setCards] = useState([]);
const [turns, setTurns] = useState(0);
console.log("Cardssssssssss")
const shuffleCards = () => {
const shuffledCards = [...cardImages, ...cardImages]
.sort(() => Math.random() - 0.5)
.map((card) => ({ ...card, id: Math.random() }))
setCards(shuffledCards);
setTurns(0) // understand sort() method
} // even though rahom bara lfunction rahom y'atfficha f onclick why
console.log("Cards:", cards)
return (
<div className="App text-center p-4 text-white bg-[black] h-[100vh] " >
<h1 className='text-4xl font-bold ' >Magic Match</h1>
<button
onClick={shuffleCards}
className='border-white border border-solid p-1 px-2 mt-4 '
>
New Game
</button>
</div