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

160
Visualizações
Call component function that inside a page from another page

I'm trying to understand how I can trigger OnSwipe from inside another JS file called CardFooter.js containing a button with OnClick triggering OnSwipe from the previous JS file called CardItem.js. They both are called inside Card.js.

I'm learning Nextjs with Reactjs components. Any insight would greatly be appreciated

CardFooter.js

import { CardItem }  from '../components/CardItem'

const CardFooter = () => { 

   return (
   
       <Button onClick={() => CardItem.onSwipe('left')}></Button>;
       <Button onClick={() => CardItem.onSwipe('right')}></Button>;

   )
}

export default CardFooter

CardItem.js

import { useState, useContext, createContext } from 'react'
import { Context } from '../context/Context'
import TinderCard from 'react-tinder-card'
import { test } from '../components/CardFooter';

export const CardItem = ({ card }) => {

  const { handleRightSwipe, handleLeftSwipe, currentAccount } = useContext(Context)

  const onSwipe = dir => {
    if (dir === 'right') {
      handleRightSwipe(card, currentAccount)
    }
    if (dir === 'left') {
      handleLeftSwipe(card, currentAccount)
    }
  } 

  return (
    <TinderCard
      preventSwipe={['up', 'down']}
      onSwipe={onSwipe}
    >
    <div style={{ backgroundImage: `url('${card.imageUrl}')` }}>
    <div>
      {card.name}
    </div>
  </div> 
</TinderCard> 
)
}

export default CardItem

Card.js

import { useContext } from 'react'
import { Context } from '../context/Context'
import CardFooter from './CardFooter'
import CardItem from './CardItem'

const Card = () => {

const { cardsData } = useContext(Context)

  return (
    <div className={style.wrapper}>
      <div className={style.cardMain}>
        <div className={style.swipesContainer}>
          {cardsData.map((card, index) => (
            <CardItem card={card} key={index} />  
          ))}
        </div>
      </div>
      <CardFooter  />
    </div>
  )
}

export default Card
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In React, you can't pass props up from child components into parent components. This means that you wouldn't be able to pass the handleSwipe function up to <CardFooter>.

But it doesn't look like you'd need to do that anyways, since you have the handleRightSwipe and handleLeftSwipe functions within your Context, and all the handleSwipe function is really doing is calling those functions.

So, with this in mind, there are two solutions:

  1. In your footer, import the context and use it, then when the user clicks on the right button, call handleRightSwipe, and handleLeftSwipe for the left button. (this is the naive solution)

  2. Define handleSwipe within the parent <Card> component, then pass it as a prop to both of the components. This way, you only need to define the function once. (ideal solution)

about 4 years ago · Juan Pablo Isaza 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