Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

430
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!