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

198
Visualizações
How do I associate seperate state to each button?

Hello

I am trying to associate a like button with each PaperCard component as shown in the code below. I have included the relevant code. Currently, The like button shows up and every time you click it the counter increases BUT all the buttons share the same state. So I am trying to fix that. I am new to JS and React.

Any help will be appreciated. Thanks!

function Home() {
  
  const [likes, setLikes] = useState(0);

  const incrementLikes = () => {
    const addToLikes = likes + 1;
    setLikes(addToLikes)
    }
  const loadMorePapers = () => {
    setVisible((prevValue) => prevValue + 3);}

  return (
    <div>
      <div style={{display:'flex', justifyContent:'center'}}>
      <h1>Latest Papers</h1>
      </div>
      {apiData.slice(0, visible).map((paper) => (
        <Grid key={paper.title}>
          <button onClick={incrementLikes}>Likes: {likes}</button>
          <PaperCard title={paper.title} abstract={paper.abstract}/>
        </Grid>
      ))}
      <div style={{display:'flex', justifyContent: 'center'}}>
      <Button variant="contained" onClick={loadMorePapers}>Load More</Button>
      </div>
      </div>
        
  )
      }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The element from the map callback is extracted as a component, and now every button has its own state.

function Home() {
  return (
    <div>
      <div style={{ display: "flex", justifyContent: "center" }}>
        <h1>Latest Papers</h1>
      </div>
      {apiData.slice(0, visible).map((paper) => (
        <LikeButton paper={paper} key={paper.title} />
      ))}
      <div style={{ display: "flex", justifyContent: "center" }}>
        <button variant="contained" onClick={loadMorePapers}>Load More</button>
      </div>
    </div>
  );
}

function LikeButton(paper) {
  const [likes, setLikes] = useState(0);

  const incrementLikes = () => {
    const addToLikes = likes + 1;
    setLikes(addToLikes);
  };

  return (
    <div key={paper.title}>
      <button onClick={incrementLikes}>Likes: {likes}</button>
      <PaperCard title={paper.title} abstract={paper.abstract}/>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Create a new functional component called LikeButton (or something relevant) to house the state for each button independently.

In that component, add the state values you want to track per each button. In your case it seems to just be the likes.

So could be something like:

const LikeButton = () => {
  const [likes, setLikes] = useState(0); //likes controlled by state of component
  const incrementLikes = () => {
    setLikes((prevState) => prevState + 1);
  };
  return <button onClick={incrementLikes}>Likes: {likes}</button>;
};

Then add that component in place of your existing button and remove the state for likes in the Home component. E.g.:

function Home() {

  const loadMorePapers = () => {
    setVisible((prevValue) => prevValue + 3);
  };

  return (
    <div>
      <div style={{ display: "flex", justifyContent: "center" }}>
        <h1>Latest Papers</h1>
      </div>
      {apiData.slice(0, visible).map((paper) => (
        <Grid key={paper.title}>
          <LikeButton/>
          <PaperCard title={paper.title} abstract={paper.abstract} />
        </Grid>
      ))}
      <div style={{ display: "flex", justifyContent: "center" }}>
        <Button variant="contained" onClick={loadMorePapers}>
          Load More
        </Button>
      </div>
    </div>
  );
}

Should you want to control state from the Home component, you can pass the likes as props, but it doesn't seem necessary for what you want.

about 4 years ago · Juan Pablo Isaza Relatório

0

In this situation you should consider using a reusable button component in order to control state within the component itself. Then you do not have to worry about the buttons sharing the same state. Here would be a simple example of a button component that will track it's count independent of the other buttons that are rendered:

import React, { useState } from 'react';

export default function CounterButton() {
    const [count, setCount] = useState(0);

    function incrementLikes() {
        setCount(count + 1);
    }

    return (
        <button onClick={incrementLikes}>
            {count} likes
        </button>
    );
}

You could simply render these buttons like in the pseudo code below:

{[0, 1, 2, 3].map((num: number, index: number) => (
      <div key={index}>
           <CounterButton />
      </div>
))}

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