본문 바로가기

Frontend/React

React Hook - Redirection (React Router DOM)

반응형

Redirection is one of the many basic features when it comes to developing a web application. React, by nature, does not have the feature but React Router DOM offers features to make the redirection availble. Let's see how we can use them

Creating a React Project

 

React

React is the most papular framework followed by Angular and Vue. React, like Angular uses components to build an application. However, React does not come with native features like Angular. Instead, it uses other libraries which makes React compact and sma

jin-co.tistory.com

Setting Up Routing

 

React Library - React Router DOM

React, by nature, does not have a routing feature. React Router DOM is one of the React library packages; with this, we can easily create routing between pages. Let's see how we can use this library. HTML 삽입 미리보기할 수 없는 소스

jin-co.tistory.com

We implement the redirection on the About route

Using Redirection

1. Navigate

This is similar to the Link tag and we use 'to' attribute to specify the path to redirect to

 

Import the Navigate

import { Navigate } from 'react-router-dom'

Set up the path to redirect

<Navigate to={'/redirect'} />

Run the app and try to access the about page, then you will be redirected to the path you specified above

2. useNavigate

This is a hook to redirect. With this option, we need to initialize 

 

Inport the hook to the component and initialize

import { useNavigate } from 'react-router-dom'

User initialized variable to specify the path to redirect (Here I used a button to fire the hook)

Run the app and click the button.

Then you will be redirected to the path you specified above

In this writing, we have seen how to use the React router DOM to redirect.


References

Traversy Media - YouTube

 

Traversy Media

Traversy Media features the best online web development and programming tutorials for all of the latest web technologies from the building blocks of HTML, CSS & JavaScript to frontend frameworks like React and Vue to backend technologies like Node.js, Pyth

www.youtube.com

 

 

 

728x90
반응형