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

Top comments (0)