Programming/React

react ์—์„œ navigate ์‚ฌ์šฉํ•˜๊ธฐ (useNavigate / Navigate)

BadaGreen_Kim 2022. 7. 23. 01:00

๐Ÿค™ Link ๋Š” ํŠน์ • ์ฃผ์†Œ๋กœ ์ด๋™ํ•ด์ฃผ๋Š” ํƒœ๊ทธ์˜€๋‹ค๋ฉด, Navigate ๋Š” ํŠน์ • ํ–‰๋™์„ ํ–ˆ์„ ๋•Œ ํ•ด๋‹น ์ฃผ์†Œ๋กœ ์ด๋™ํ•ด์ค„ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 


1. useNavigate ์“ฐ๊ธฐ

 

useNavigate๋Š” ์–‘์‹์ด ์ œ์ถœ๋˜๊ฑฐ๋‚˜ ํŠน์ • event๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ,  url์„ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” interface๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

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

function Func() {
  const navigate = useNavigate();

  const onClickImg = () => {
    navigate(`/comment/id/๋“ฑ๋“ฑ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ฃผ์†Œ`);
  };

  return (
  	<img src={...} alt={...} onClick={onClickImg} />
  );
}

export default Func;

 

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ์—์„œ์˜ url์ด www.naver.com ์ด์—ˆ๋‹ค๋ฉด ์ด๋Š” www.naver.com/comment/id... ์œผ๋กœ ๋ฐ”๋€Œ์—ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

useNavigate์—๋Š” ๋‘ ๊ฐ€์ง€ ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 


(1) useNavigate์˜ ์ธ์ž๋“ค

 

navigate("../success",  { replace: true});

 

์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ๋Š” ์ฃผ์†Œ๋ฅผ ๋ฐ›์œผ๋ฉฐ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ { replace, state } ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

  • replace (true or false)

true๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, navigate์— ์ ํžŒ ์ฃผ์†Œ๋กœ ๋„˜์–ด๊ฐ„ ํ›„ ๋’ค๋กœ๊ฐ€๊ธฐ๋ฅผ ํ•˜๋”๋ผ๋„ ๋ฐฉ๊ธˆ์˜ ํŽ˜์ด์ง€๋กœ ๋Œ์•„์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๋•Œ๋Š” ์ž์‹ ์˜ ๋ฉ”์ธ ํŽ˜์ด์ง€ ("/")๋กœ ๋Œ์•„์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. false๋Š” ๋’ค๋กœ๊ฐ€๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉฐ ์ด ๊ฒƒ์ด ๊ธฐ๋ณธ ๊ฐ’์ธ ๋“ฏํ•ฉ๋‹ˆ๋‹ค.

 

  • state (any)

state๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค๋งŒ... ์ €๋Š” ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€๋„ ์ž˜ ๋ชจ๋ฅด๊ฒ ๊ณ  ์ œ๋Œ€๋กœ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋„ ์—†๋”๊ตฐ์š”...ใ…œ

 


(2) useHistory์˜ ๊ธฐ๋Šฅ๋„ ํฌํ•จ

 

useNavigate๋Š” react v6 ์—์„œ useHistory ๊ฐ€ ๋ณ€ํ™”ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋•Œ useHistory ์—์„œ ์‚ฌ์šฉํ•˜๋˜, window์˜ history ๋ฅผ ์ด์šฉํ•œ navigate ๊ธฐ๋Šฅ๋„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

<>
  <button onClick={() => navigate(-2)}>
    Go 2 pages back
  </button>
  <button onClick={() => navigate(-1)}>
    Go back
  </button>
  <button onClick={() => navigate(1)}>
    Go forward
  </button>
  <button onClick={() => navigate(2)}>
    Go 2 pages forward
  </button>
</>

 

History๋ž€ ์„ธ์…˜ ๊ธฐ๋ก(ํŽ˜์ด์ง€ ๋ฐฉ๋ฌธ ๊ธฐ๋ก)์— ๋Œ€ํ•œ ์ ‘๊ทผ ๋ฐฉ๋ฒ•๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์„ธ์…˜(session)์ด๋ž€ ์›น ์‚ฌ์ดํŠธ์˜ ์—ฌ๋Ÿฌ ํŽ˜์ด์ง€์— ๊ฑธ์ณ ์‚ฌ์šฉ๋˜๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์˜๋ฏธํ•˜์ง€์š”. ๊ฐ„๋‹จํ•˜๊ฒŒ๋Š” ๊ทธ๋ƒฅ ์ด๊ฑธ๋กœ ๋’ค๋กœ๊ฐ€๊ธฐ ์•ž์œผ๋กœ๊ฐ€๊ธฐ๊ฐ€ ๋œ๋‹ค๊ณ  ์•Œ๊ณ  ๊ณ„์‹œ๋ฉด ๋˜๋Š”๋ฐ ๋”์šฑ ์•Œ๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด ์–ด์ฉ” ์ˆ˜ ์—†์ด ์•„๋ž˜ ํŽ˜์ด์ง€๋กœ ๊ฐ‘๋‹ˆ๋‹ค.

2022.01.21 - [๐Ÿ“ฒ For Front/๐Ÿ“ฒFront Knowledge] - Web History ๋ž€ ๋ฌด์—‡์ธ๊ฐ€? (History API)

 

 


2. Navigate ์“ฐ๊ธฐ

 

<Navigate> ์š”์†Œ๋Š” ๋ Œ๋”๋ง๋  ๋•Œ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. useNavigate๋ฅผ ๋‘˜๋Ÿฌ์‹ผ component wrapper ์ด๋ฉฐ props์™€ ๋™์ผํ•œ ๋ชจ๋“  ์ธ์ˆ˜๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ํƒœ๊ทธ ์—ญ์‹œ to, replace, state ๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

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

function Func() {

    const onClickImg = () => {
    return <Navigate to="/login" />;
  }

  return (
  	<img src={...} alt={...} onClick={onClickImg} />
  );
}

export default Func;

 

Note: This API is mostly useful in React.Component subclasses that are not able to use hooks. In functional components, we recommend you use the `useNavigate` hook instead.
-์ฐธ๊ณ  : ์ด API๋Š” ๋Œ€๋ถ€๋ถ„ ํ›„ํฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” React.Component ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋Šฅ์  ๊ตฌ์„ฑ ์š”์†Œ์—์„œ๋Š” ๋Œ€์‹  `useNavigate` ํ›„ํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.-

 

๋ผ๊ณ  ๊ณต์‹ ํ™ˆํŽ˜์ด์ง€์—์„œ๋Š” ์–˜๊ธฐํ•ฉ๋‹ˆ๋‹ค. ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ์œ„ํ•ด์„œ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ด๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฅ๊ฐ„ํ•ด์„œ๋Š” useNavigate๋ฅผ ์“ฐ๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 


< ์ถœ์ฒ˜ >

[React] react-router-dom v6 ์—…๊ทธ๋ ˆ์ด๋“œ ๋˜๋ฉด์„œ ๋‹ฌ๋ผ์ง„ ๊ฒƒ๋“ค (https://velog.io/@soryeongk/ReactRouterDomV6)

navigate ๊ณตํ™ˆ (https://reactrouter.com/docs/en/v6/api#navigate)

useNavigatge ๊ณตํ™ˆ (https://reactrouter.com/docs/en/v6/api#usenavigate)

 

๋ฐœ์ทŒ ๋ธ”๋กœ๊ทธ https://basemenks.tistory.com/278