DEV Community

Cover image for Play with the React Router Part-2
Sai gowtham
Sai gowtham

Posted on • Edited on

8 5

Play with the React Router Part-2

How to Handle the 404 Requests and Active Styles in React Router.

If you want to start from the First Part Refer Play With the React 🐘Router.

How to apply active styles whenever URL/path matches in React Router?

  • NavLink component is available for styling the Nav items.
  • we need to use NavLink Instead Of Link Component.

React Router

Two Properties are available for styling.

  • activeClassName
  • activeStyle.

activeClassName: we need to pass the class name.
activeStyle: we need to pass the styles as an object.

Now let's see activeStyle property in Practice.

activeStyle

How to Handle 404 requests in React Router?

We need to wrap the all of our routes with Switch component.

Switch: It helps us to render the Component only when particular path/URL matches.

In above code, only two routes are there so that switch only renders that component whenever the URL matches. If any of the URL doesn't matches switch renders the Not found Component.

Final Output.

Happy coding...

Play With the React 🐘Router Part 1

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more