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

431
Visualizações
how to save an object using a button in reactjs

Currently, I have a WebSocket chat messager using react js, I want to have it save the message object when I click on its associated button so that it will pin it. How do I get a button press to save the object it's associated with?

Currently, I have an array of objects that just stack on top of each other like so:

{messages.map(message => (
        <>
           <tr>
              <td>{message.message}</td>
              <td><button id="pin_button" type="button" >Pin Message</button></td>
           </tr>
        </>
))}

What I want to do is have it when I press that button it will save that object and preferably send it to a WebSocket so that other people can see the message that was pinned

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

0

You should use state.

const [savedMsgs, setSavedMsgs] = useState([]);

<ul className="saved-msgs">
  {savedMsgs.map((msg,i) => <li key={i}>{msg.message}</li>)}
</ul>

<tr>
  <td>{message.message}</td>
  <td>
    <button
      id="pin_button"
      onClick={() => setSavedMsgs([...new Set([...savedMsgs, message])])}
    >
      Pin Message
    </button>
  </td>
</tr>

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