DEV Community

Walktheworld
Walktheworld

Posted on

Persist data with like button React

I created a like button that I could use in two different routes to persist data to a third route.

First I created the LikeBtn class and set its state of liked to false so I could change that status to change the color after it has been clicked.

Alt Text

Next, I gave the button a handleClick function to change that state of liked from false to true. Then I used a GET request and the POST method to persist that data to my localhost server.

Alt Text

Finally, I use render to change the style color and the onClick functionality and render and all other properties of the button.

Alt Text

Now, let's take a look at the full code.

Alt Text

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay