Apps Development, Cloud Computing

4 Mins Read

A Guide to Implement Dynamic Routing and Slugs in ReactJS and Strapi

Voiced by Amazon Polly

Overview

Web app development has advanced, giving users more personalized and lively experiences. React, this well-liked JavaScript library for creating user interfaces, works great with Strapi, a handy open-source headless CMS, to offer a strong way to handle content and add dynamic features like routing and slugs. In this guide, we will explore on how to set up routing and slugs in a React app with Strapi as your backend. So, whether you’re making a blog, an e-commerce site, or any app that needs dynamic content, this tutorial covered you with the tools you need to manage and showcase your content easily.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Dynamic Routing and Slug

Dynamic routing is a way in web development where the paths to get to different website sections are figured out while the site runs instead of being fixed from the start. This makes it easier to develop user-friendly URLs and is also good for search engines.

A slug is just a part of a URL that points to a specific page on a website, and it’s usually something that people can read easily, often coming from the page title. For instance, in the URL www.example.com/blog/how-to-code, “how-to-code” is the slug.

Dynamic routing and slugs are super important for websites that focus on content because they help with easy navigation and can boost SEO, which means the content becomes simpler to find and more interesting for users.

Step-by-Step Process

Step 1: Set Up Your Development Environment

Before you get going, ensure you have Node.js and npm installed on your computer. You’ll also want to have both React and Strapi projects created. If you haven’t, go ahead and make them with these commands:

  1. Create React App:

step1

2. Create Strapi App:

step1b

Step 2: Set Up Strapi for Managing Content

Define Content Types:

  • Go to your Strapi admin panel (http://localhost:1337/admin) and create a new content type (like Articles).
  • Include fields such as Title, Content, and Slug. Ensure the Slug field is unique for every entry since it’ll be used for dynamic routing.

Add Some Content:

  • Put in a few sample data for your new content type so you can work with it.

Step 3: Setting Up API Endpoints

Strapi automatically generates RESTful API endpoints for your content types. You can customize these endpoints if needed, but you can access your content at routes like /articles for basic usage.

Step 4: Getting Data in React

  1. Install Axios:

To fetch data from Strapi, install Axios in your React app:

step4

2. Create a Service for API Calls:

In your React app, create a folder named services and add a file api.js to manage API calls:

step4b

Step 5: Do Dynamic Routing in React

Get React Router:

  • React Router is key for managing routes in your React apps:

step5

Set Routes Up:

  • In your js (or wherever you handle routes), set up the routes for managing dynamic paths:

step5b

Make Components:

  • HomePage Component: Get and show a list of articles along with links to their article pages.
  • ArticlePage Component: Get and show the article details based on the slug in the URL.

HomePage.js:

step5c

ArticlePage.js:

step5d

Step 6: Test Your App

Make sure to run your Strapi and React apps to check if everything works.

  • To start Strapi, use npm run develop while in the Strapi folder.
  • To start React, type npm run dev in the React folder.

Then, head over to your React app in the browser and click on the article links to test the dynamic routing.

Benefits

  • SEO Optimization: Slugs make URLs more user-friendly and descriptive, which helps boost search engine rankings.
  • Scalability: You can easily add more types of content and routes without changing the current code.
  • User Experience: It gives users a smooth navigation experience.
  • Flexibility: Makes it simple to manage and update content with Strapi.

Conclusion

Using dynamic routing and slugs in your React app with Strapi as the backend, you’ve set up a flexible, scalable way to manage and show dynamic content. This setup helps with SEO and improves the user experience since it gives clean and easy-to-navigate URLs.

Drop a query if you have any questions regarding React app with Strapi and we will get back to you quickly.

Making IT Networks Enterprise-ready – Cloud Management Services

  • Accelerated cloud migration
  • End-to-end view of the cloud environment
Get Started

About CloudThat

CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFront and many more.

To get started, go through our Consultancy page and Managed Services PackageCloudThat’s offerings.

FAQs

1. Can I use GraphQL together with Strapi and React?

ANS: – Sure, Strapi does support GraphQL, so you can use it instead of RESTful APIs when fetching data in your React app.

2. How can I ensure the data from Strapi in my React app is secure?

ANS: – You should set up authentication in Strapi and use secure methods in your React app to ensure that only authorized users can access the data.

WRITTEN BY Anirudh Singh

Anirudh Singh works as a Research Intern at CloudThat. He is passionate about learning new technologies in Cloud and exploring them.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!