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

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️