Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda