๐ŸŒ— The Art of React part4

hwakyungChoiยท2021๋…„ 1์›” 9์ผ
0
post-thumbnail

๐Ÿณ ํ”„๋กœ์ ํŠธ ์ •๋ฆฌ

  • ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๊ธฐ๋ณธ์ ์ธ ํ”„๋ก ํŠธ์—”๋“œ์˜ ์ž‘์—… ํ™˜๊ฒฝ ์„ค์ •์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • ๋ผ์šฐํŠธ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” react-router-dom ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Router๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • path์˜ @ ์ž…๋ ฅํ•˜๋ฉด ๊ฒฝ๋กœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.(Medium,๋ธŒ๋Ÿฐ์น˜์™€ ๊ฐ™์€ ๊ณณ์—์„œ ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉ)
  • ์Šคํƒ€์ผ ์„ค์ •์„ ์œ„ํ•ด styled-components๋ฅผ ์ด์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • styled-components๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์•ˆ์—์„œ CSS ์„ค์ •์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.
  • ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ ์ปดํฌ๋„ŒํŠธ์˜ ๊ฒฝ์šฐ์—๋Š” ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค importํ•˜์—ฌ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค๊ณ  ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • redux๋ฅผ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ธ๋ฐ ๋ฆฌ๋•์Šค๋Š” ๋ฆฌ์•กํŠธ๋ฅผ ๋ณด์™„ํ•ด์ฃผ๋ฉฐ action์— ๋ฐ˜์‘ํ•˜์—ฌ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•ด์ค๋‹ˆ๋‹ค.
  • ๋ฆฌ๋•์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋ฆฌ์•กํŠธ์˜ local state์™€ global state๊ฐ€ ๊ด€๋ฆฌํ•˜๊ธฐ ์–ด๋ ค์šด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ™ ์ฝ”๋“œ

//App.js
import './App.css';
import React from "react";
import { Route } from "react-router-dom";
import PostListPage from "./pages/PostListPage";
import LoginPage from "./pages/LoginPage";
import PostPage from "./pages/PostPage";
import RegisterPage from "./pages/RegisterPage";
import WritePage from "./pages/WritePage";

const App = () => {
  return (
    <>
      {/* "/@:username"  username ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ */}
      <Route component={PostListPage} path={["/@:username", "/"]} exact />
      <Route component={LoginPage} path={["/login"]} />
      <Route component={RegisterPage} path={["/register"]} />
      <Route component={WritePage} path={["/write"]} />
      <Route component={PostPage} path={["/@:username/:postId"]} />
    </>
  )
}

export default App;


//AuthForm.js
import React from "react";
import styled from "styled-components";
import { Link } from "react-router-dom";
import palette from "../../lib/styles/palette";
import Button from "../common/Button";

//  ํšŒ์›๊ฐ€์ž… ๋˜๋Š” ๋กœ๊ทธ์ธ ํผ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
const AuthFormBlock = styled.div`
    h3 {
        margin:0;
        color:${palette.gray[8]};
        margin-bottom:1rem;
    }
`;
(...)


const AuthForm = () => {
    return (
        <AuthFormBlock>
            <h3>๋กœ๊ทธ์ธ</h3>
            <form>
                <StyledInput autoComplete="username" name="username" placeholder="์•„์ด๋””" />
                <StyledInput autoComplete="new-password" name="password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" type="password" />
                <Button>๋กœ๊ทธ์ธ</Button>
            </form>
            <Footer>
                <Link to="/register">ํšŒ์›๊ฐ€์ž…</Link>
            </Footer>
        </AuthFormBlock>
    );
};

export default AuthForm;

์ฐธ์กฐ : ๋ฆฌ์•กํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ 

0๊ฐœ์˜ ๋Œ“๊ธ€