Front End/React

[React] Attempted import error: 'Redirect' is not exported from 'react-router' (리액트 redirect 안됨)

DevPing9_ 2021. 12. 1. 19:26

어서오세요..! 🤗

<Routes></Routes> 해결하시고 또 뵙는거겠죠....? 

(나쁜놈들,,,)

 

react-router-dom 이 V5 -> V6 로 버전업되면서 변경된 사항입니다.

 

 

#  변경사항

  • <Redirect/> 컴포넌트는 더 이상 존재하지 않음
  • <Navigate/> 로 바뀜 (근데 이건 제가 잘못 사용한건지 먹히질 않더라구요 😭)
  • useNavigate() Hook 릴리즈..! (이건 잘 먹힙니다..!!)

 

# 코드 구현

import React, {useState} from "react";
import { useNavigate } from "react-router";

export default function SignIn(props){

    const navigate = useNavigate(); /// ....

    const click = (e)=>{
        console.log("what the heck...;;;");
        navigate("/home"); // 이렇게 하시면 됩니다..... ㅎ..... 하ㅏ... 하ㅏㅏ하하ㅏㅎ
    };

    return(
        <div>
            <button onClick={click}>redirect.................</button>
        </div>        
    );
}

 

 

# 나는 Route 부터 막히는데...? 😢

 

[React] A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your <Route> in

ㅋㅋㅋㅋㅋㅋㅋㅋ 에러문구가 시키는대로 로 감싸주었는데도 해결 못하셔서 찾아오신분들 환영합니다. 'react-router-dom' 라이브러리가 버전 업데이트가 됬어요...! 의 매개변수 이름이 변경되었습

developer-ping9.tistory.com

 

 

 

 

 


# Reference

 

Notes on handling redirects in React Router v6, including a detailed explanation of how this improves on what we used to do in v

Notes on handling redirects in React Router v6, including a detailed explanation of how this improves on what we used to do in v4/5 - redirects-in-react-router-v6.md

gist.github.com

 

 

 

How to upgrade React Router v5 to v6

React Router version 6 introduces several powerful new features, as well as improved compatibility with the latest versions of React. In this React Router tutorial is a comprehensive guide on how to upgrade your React Router v4/5 app to React Router v6. Re

morioh.com

 

728x90