Javascript/React

[React] ๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ์™€ ๊ด€๋ จ Hooks ์ •๋ฆฌ

Rainbow๐ŸŒˆCoder 2022. 12. 14. 12:37
728x90

 

- ๋ผ์šฐํ„ฐ ๊ฐœ๋…์ดํ•ด + Link ๊ฐœ๋…

- restful route ๊ฐœ๋… ์ดํ•ด

- react hook ์ค‘์—์„œ useNavigate, useParams, useSearchParams์ดํ•ด

 

 

Route๋Š” ๊ฒฝ๋กœ๋ž€ ๋œป์ด๋‹ค. ์›นํŽ˜์ด์ง€๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ๋งŒ๋“ค๊ณ , ์ด ๊ฐ๊ฐ์˜ ์›นํŽ˜์ด์ง€๋ฅผ ๊ฐ€๊ธฐ ์œ„ํ•œ ๊ฒฝ๋กœ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

 

์„ค์น˜ ๋ช…๋ น์–ด

npm install react-router-dom
 

 

BrowserRouter

๋‹ค๋ฅธ ์ฐฝ์˜ URL์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ถ”์ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค

import * as React from "react";
import * as ReactDOM from "react-dom";
import { BrowserRouter } from "react-router-dom";

ReactDOM.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  root
);
 

 

Route

๊ฐ๊ฐ์˜ ํŽ˜์ด์ง€๋ฅผ ๋ผ์šฐํ„ฐ๋กœ ๊ฐ์‹ผ๋‹ค.

path์—๋Š” ์ฃผ์†Œ์ฐฝ์— ์“ธ ๊ฒฝ๋กœ, element์—๋Š” ํŽ˜์ด์ง€ ์ปดํฌ๋„ŒํŠธ ์ž…๋ ฅ

import { Route } from "react-router-dom";

<Route path="/teams" element={<Teams />}>
  <Route path=":teamId" element={<Team />} />
</Route>
 

Link

์•ต์ปค์ฒ˜๋Ÿผ ์ž‘๋™ํ•œ๋‹ค. ๋ผ์šฐํ„ฐ ๊ฐ„ ์ด๋™์„ ๋„์™€์ฃผ๋Š” ๋งํฌ๋ผ๊ณ  ํ•ด์„ํ•˜๋ฉด ๋œ๋‹ค.

 

function goAbout() {
  return (
    <Link to="/about">
      Go to about page
    </Link>
  );
}
 

useNavigate

() ์•ˆ์— ๋‚ด๊ฐ€ ๊ฐ€๊ณ  ์‹ถ์€ ๊ฒฝ๋กœ๋ฅผ ์ž‘์„ฑํ•ด์ค€๋‹ค.

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

function useLogoutTimer() {
  const userIsInactive = useFakeInactiveUser();
  const navigate = useNavigate();

  useEffect(() => {
    if (userIsInactive) {
      fake.logout();
      navigate("/session-timed-out");
    }
  }, [userIsInactive]);
}
 

 

Restful Routes

๋Œ€ํ‘œ์‚ฌ์ง„ ์‚ญ์ œ

์‚ฌ์ง„ ์„ค๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š”.

 

useParams

'/' ๋กœ ์ด์–ด์ง„ ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋Š” ์„ฑ์งˆ์„ ์ด์šฉํ•˜์—ฌ URL์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

import * as React from 'react';
import { Routes, Route, useParams } from 'react-router-dom';

function ProfilePage() {
  // Get the userId param from the URL.
  let { userId } = useParams();
  // ...
}

function App() {
  return (
    <Routes>
      <Route path="users">
        <Route path=":userId" element={<ProfilePage />} />
        <Route path="me" element={...} />
      </Route>
    </Routes>
  );
}
 

useSearchParams

์ฟผ๋ฆฌ๊ฐ’์„ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

import * as React from "react";
import { useSearchParams } from "react-router-dom";

function App() {
  let [searchParams, setSearchParams] = useSearchParams();

  function handleSubmit(event) {
    event.preventDefault();
    // The serialize function here would be responsible for
    // creating an object of { key: value } pairs from the
    // fields in the form that make up the query.
    let params = serializeFormQuery(event.target);
    setSearchParams(params);
  }

  return (
    <div>
      <form onSubmit={handleSubmit}>{/* ... */}</form>
    </div>
  );
}
 

 

 

 

๊ธฐํƒ€ hooks๋“ค์€ ๋„ํ๋จผํŠธ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฝ์œผ๋ฉด์„œ ์ ์šฉํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค :)

 

 

 

์„ค๋ช…

 

๋ผ์šฐํ„ฐ๋Š” '์–ด๋–ค ํ™”๋ฉด์„ ๋ณด์—ฌ ์ค„์ง€ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ์นœ๊ตฌ'์ด๋‹ค.

์กฐ๊ธˆ ๋” ์ •ํ™•ํ•˜๊ฒŒ ๋งํ•˜์ž๋ฉด '์–ด๋–ค ์ปดํฌ๋„ŒํŠธ'๋ฅผ '๋ Œ๋”๋ง'ํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

(์ปดํฌ๋„ŒํŠธ๋Š” ํ™”๋ฉด์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฐ์ด๋‹ค.)

 

 

 

 

 
Home v6.4.5

What's New in 6.4? v6.4 is our most exciting release yet with new data abstractions for reads, writes, and navigation hooks to easily keep your UI in sync with your data. The new feature overview will catch you up. I'm New Start with the tutorial. It will quickly introduce you to the primary feature...

reactrouter.com

 

React Router ๋„ํ๋จผํŠธ

 

Home v6.4.5

I'm on v5 The migration guide will help you migrate incrementally and keep shipping along the way. Or, do it all in one yolo commit! Either way, we've got you covered to start using the new features right away.

reactrouter.com

 

 

 

 

728x90