tag

Creating a Blogging App Using React, Part 6: Tags

In the previous part of this tutorial series, you saw how to implement the profile page feature for the React blog application. In this tutorial, you’ll see how to implement tags for the blog posts.

Getting Started

Let’s get started by cloning the source code from the last part of the series.

Once the directory has been cloned, navigate to the project directory and install the required dependencies.

Start the Node.js server and you will have the application running at http://localhost:7777/index.html#/.

Creating the Tag Page

Let’s start by creating a link for the user to add a tag to the MongoDB database. In the index.html page, add one more li for the Add Tag page.

When the user clicks the Add Tag link, it should display the AddTag React component. So let’s add a route for the Add Tag component in the home.jsx file.

Here is the complete route list:

Let’s create the AddTag component which will be rendered when the user clicks the Add Tag link. 

As seen in the above code, inside the AddTag react component class you have rendered the basic HTML template for the page. Inside the componentDidMount method you have class name to make the AddTag hyperlink active.

Save the above changes and restart the server. Sign into the application and click on the Add Tag link, and you will be able to view the Add Tag page.

React Blog - Add Tag View

Implementing the Add Tag Functionality

Define a state variable to keep track of the tag change.

Attach the tag state variable to the input element in the render method.

As seen in the above code, you have also attached an onChange event to the input to keep track of its value change. Bind the onChange method handleTagChange inside the constructor.

Now let’s define the handleTagChange method inside the AddTag React component.

When the user clicks the Add button to add the tag, you need to save the data. So let’s attach an onClick event to the input button.

Bind the addTag method inside the React component constructor and define the method to make an API call to the Node.js server endpoint.

Next let’s define the addTag method to make the API call to the /addtag end point.

Let’s create the Node.js API endpoint for /addTag. Inside the app.js file, create the /addTag route, which will parse the data and insert the data into the MongoDB database.

Inside the /addTag endpoint, you have a made a call to a method called addTag from the post.js file. Let’s create the addTag method inside the post.js file. Here is how it looks:

As seen in the above code, you have used MongoClient to connect to the MongoDB database. You have inserted the tag data into a collection called tag inside the database. Once the data has been inserted without any error, Boolean true is passed to the callback function. If an error is encountered, a Boolean false is returned to the callback function.

Save the above changes and restart the server. Sign in to the app and click on the Add Tag link. Enter a tag name and click the Add button. Check the browser console and you should be able to see the success message logged in the browser console. 

Populating the Tags on the Add Post Page

Once you have added the tags from the Add Tag page, tags need to be populated in the Add post page. Let’s start by creating a method called getTags inside the post.js file which would connect to the MongoDB database and fetch the tags. Here is how it looks:

As seen in the above code, you have used the MongoClient to connect to the MongoDB database. Once the collection has been fetched, it’s been converted to array using the toArray method and then passed to the callback function. 

Next create the Node.js API endpoint which will call the above getTag method in app.js

In the home.jsx file inside the AddPost component, create a method called getTags which will make the /gettag API call and fetch the tag list. 

Once the data has been fetched from the API, set the data inside the tags state variable.

Inside the render method of the ShowPost React component, add the select HTML element to bind the tags state variable. Here is how it would look:

As seen in the above code, you have used the map method to map the tags state variable to the select element.

Inside the componentDidMount method, make a call to the getTags method to load tags once the component has mounted.

Save the above changes and restart the server. Once you have loaded the Add Post page, you should have the tags loaded in a select HTML element.

React Blog App - Category Loaded in Add Post Page

Let’s add a default option in the drop-down with value 0.

You have added an onChange event to the select HTML element. Here is what the handleTagChange event looks like:

Once the user selects the tag, the value will be available in the state variable tag

Include the tag variable in the addPost method in the AddPost React component. 

Change the /addpost API endpoint to include the tag parameter.

Modify the addPost method in the post.js file to include the tag parameter.

Modify the getPostWithId method to set the drop-down when the post detail is fetched.

Save the above changes and restart the server. Sign in and navigate to the Add Post page and add a post with a tag selected. You will have the new post saved and listed. Click on the edit button to edit the post details and tag. 

Wrapping It Up

In this tutorial you saw how to implement the add tag page. You added the tag field to the add post page along with title and subject fields. 

Hope you enjoyed this tutorial series. Do let us know your thoughts, suggestions or any corrections in the comments below.

Source code from this tutorial is available on GitHub.

Powered by WPeMatico

Leave a Comment

Scroll to Top