Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

91
Visualizações
Conditional Render react-card-flip children based on a button or else 1 frontside 2 backsides

is the following im trying to do possible with the react-flip-package?

Basically my front side is a card with 2 buttons. When i click the one button id like to flip into one backside and if i click to the other button i want to flip on another different backside.

https://codesandbox.io/s/cool-sunset-pfjjln?file=/src/App.js

Heres my code.

import CardContent from '@mui/material/CardContent'
import { IconButton, Box } from '@mui/material'
import TrainIcon from '@mui/icons-material/Train'
import ExpandMoreIcon from '@mui/icons-material/ExpandMore'
import ReactCardFlip from 'react-card-flip'
import CardDestination from './CardDestination'
import CardBrowsePlan from './CardBrowsePlan'
import React from 'react'

function CardTrial() {
    const [isFlipped, setIsFlipped] = React.useState(false)

    const handleClick2 = () => {
        setIsFlipped(!isFlipped)
    }

    return (
        <ReactCardFlip isFlipped={isFlipped} flipDirection="horizontal">
            {/* front */}
            
                <Card>
                    <CardContent>
                        <IconButton onClick={handleClick2}>
                            <TrainIcon sx={{ height: 25, width: 25 }} />
                        </IconButton>

                        <IconButton onClick={handleClick2}>
                            <ExpandMoreIcon sx={{ height: 25, width: 25 }} />
                        </IconButton>
                    </CardContent>
                </Card>
            

            {/* back */}

            <Box>
                <CardDestination flip2={handleClick2} />
                <CardBrowsePlan flip2={handleClick2} />
            </Box>
        </ReactCardFlip>
    )
}

export default CardTrial

Right now i have my children in a box and obviously they both turn when i click any of the 2 buttons. (also passing props so they can turn back) How do i go about this? Thanks!

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can make one more state to tell React which card have to be flipped.

function CardTrial() {
    const [isFlipped, setIsFlipped] = React.useState(false)
    const [isDestinationCard, setIsDestinationCard] = React.useState(false)

    const handleClick1 = () => {
        setIsDestinationCard(true)
        setIsFlipped(!isFlipped)
    }
    const handleClick2 = () => {
        setIsDestinationCard(false)
        setIsFlipped(!isFlipped)
    }

    return (
        <ReactCardFlip isFlipped={isFlipped} flipDirection="horizontal">
            {/* front */}
            
                <Card>
                    <CardContent>
                        <IconButton onClick={handleClick1}>
            {/* this button will open CardDestination */}
                            <TrainIcon sx={{ height: 25, width: 25 }} />
                        </IconButton>

                        <IconButton onClick={handleClick2}>
            {/* this button will open CardBrowsePlan */}
                            <ExpandMoreIcon sx={{ height: 25, width: 25 }} />
                        </IconButton>
                    </CardContent>
                </Card>

            {/* back */}

            <Box>
                {isDestinationCard ? <CardDestination flip2={handleClick1} /> : <CardBrowsePlan flip2={handleClick2} />}
            </Box>
        </ReactCardFlip>
    )
}

export default CardTrial

Or, you can make your state as a string which will tell you what card to open or will close on empty value

function CardTrial() {
    const [CardTrigger, setCardTrigger] = React.useState("")

    const handleClick = card => {
        setCardTrigger(card)
    }
    return (
        <ReactCardFlip isFlipped={!!CardTrigger.length} flipDirection="horizontal">
            {/* front */}
            
                <Card>
                    <CardContent>
                        <IconButton onClick={() => handleClick("destination")}>
            {/* this button will open CardDestination */}
                            <TrainIcon sx={{ height: 25, width: 25 }} />
                        </IconButton>

                        <IconButton onClick={() => handleClick("browsePlan")}>
            {/* this button will open CardBrowsePlan */}
                            <ExpandMoreIcon sx={{ height: 25, width: 25 }} />
                        </IconButton>
                    </CardContent>
                </Card>

            {/* back */}

            <Box>
                {CardTrigger === "destination" && <CardDestination flip2={() => handleClick("")} />}
                {CardTrigger === "browsePlan" && <CardBrowsePlan flip2={() => handleClick("")} />}
            </Box>
        </ReactCardFlip>
    )
}

export default CardTrial
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda