Пробую Реакт, хочу написать card memory game. При нажатии на компонент карточки не появляется текст (
data), но если попробовать отлогировать - методы выполняются. В чем может быть проблема?
Field.js
import React, {useState} from 'react';
import styled from "styled-components";
import Card from "./Card";
import {cardsData, shuffle} from "../common/cards.data";
const FieldGrid = styled.div`
display: grid;
grid-gap: 2rem;
grid-template-columns: repeat(4, 1fr);
`
const Field = () => {
const [cards, setCards] = useState(() => shuffle(cardsData))
const [isFlipped, setIsFlipped] = useState(Array<boolean>(cards.length).fill(false))
const handleFlip = (index: number) => {
isFlipped[index] = !isFlipped[index]
setIsFlipped(isFlipped)
}
return (
<FieldGrid>
{
cards.map((data, index) => {
return <Card key={index}
index={index}
isFlipped={isFlipped[index]}
data={data}
onClick={handleFlip}/>
})
}
</FieldGrid>
);
};
export default Field;
Card.js
import React from 'react';
import styled from 'styled-components'
import CardProps from "../common/interfaces/card.props";
const CardWrapper = styled.div`
background: #fbfdfd;
border-radius: 12px;
width: 10rem;
height: 10rem;
padding: 3rem;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 4px 8px 0 rgba(176, 192, 204, 0.2);
font-size: 3rem;
text-align: center;
transition: 150ms background-color ease;
cursor: pointer;
&:hover {
background: #f2f8f8;
}
`
const Card = (props: CardProps) => {
const handleClick = () => {
props.onClick(props.index);
};
return (
<CardWrapper onClick={handleClick}>
{props.isFlipped ? props.data : ""}
</CardWrapper>
);
};
export default Card;
export default interface CardProps {
data: string;
isFlipped: boolean;
index: number;
onClick: (index: number) => void;
}