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

98
Visualizações
How to call a function in one React component by button click in another component

I am making a pos system and I am trying to make the items appear on the list section when I click on each of them. My problem is that the Items section and the List section are two different components and I can not figure out how to make a click in Items section change the state of the list section.

Items Section:

import Item from "./Item";

function ItemSection() {
  const Items = [{ title: "DOMAIN", cost: 109 }];
  return (
    <div>
         <Item title={Items[0].title} cost={Items[0].cost} onClick={() => this.clickHandler(title, cost)}></Item>
    </div>
  );
}

export default ItemSection;

List Section:

import React, { useState } from "react";

function ListItems(props) {
  const [title, setTitle] = useState(props.title);
  const [cost, setCost] = useState(props.cost);

  const clickHandler = (mTitle, mCost) => {
    setTitle(mTitle);
    setCost(mCost);
  };

  return (
    <div>
      <table>
        <tr>
          <td> {title} </td>
          <td> ${cost} </td>
        </tr>
      </table>
    </div>
  );
}

export default ListItems;

Item:

function Item(props) {

  return (
    <div>
      <button>{props.title}</button>
    </div>
  );
}

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

0

As per my understanding, you've two components: ItemSection and ListItems and you want to call a function in ListItems, that is defined in ItemSection? Is it the correct understanding?

If yes, then you can define your function in ItemSection and send that function as prop in ItemSection

In ItemSection:

    <ListItems sampleFunction = {(e)=>sampleFunction(e)}/>

In ListItems:

    interface Props{
    sampleFunction: Function;
    }

and:

    <button OnClick = {props.sampleFunction}>{props.title}</button>

This is just a pseudo code.

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