Skip to content

kidol7/book_shelf

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

16 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Book Shelf

๐Ÿ“š Book Shelf Web Application in TypeScript, React and Redux.


1. ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์ดˆ๊ธฐํ™”

npx create-react-app . --template typescript

2. ๋ผ์šฐํŒ…์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค€๋น„

yarn add react-router-dom
yarn add @types/react-router-dom -D # ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์œ„ํ•จ

3. ์—๋Ÿฌ ํ•ธ๋“ค๋ง ํŽ˜์ด์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค€๋น„

yarn add react-error-boundary # componentDidPatch๋กœ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ ํ•ธ๋“ค๋ง โ†’ ์—๋Ÿฌ ํŽ˜์ด์ง€๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
import { ErrorBoundary } from "react-error-boundary";
import { BrowserRouter, Switch, Route } from "react-router-dom";
import Error from './pages/Error';

<ErrorBoundary FallbackComponent={Error}>
  <BrowserRouter>
    <Switch>
      <Route />
        {/* ... */}
    </Switch>
  </BrowserRouter>
</ErrorBoundary>

4. ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ Redux ์ค€๋น„

yarn add redux react-redux redux-saga redux-devtools-extension redux-actions
yarn add @types/{react-redux,redux-actions} -D
.
โ””โ”€โ”€ src
    โ””โ”€โ”€ redux
        โ”œโ”€โ”€ create.ts
        โ””โ”€โ”€ modules
            โ”œโ”€โ”€ reducer.ts
            โ”œโ”€โ”€ auth.ts
            โ””โ”€โ”€ rootSaga.ts

5. ๋””์ž์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Ant Design ์ค€๋น„

yarn add antd
yarn add @ant-design/icons
// index.tsx
import 'antd/dist/antd.css';

6. ๋กœ๊ทธ์ธ ์„ค๊ณ„

6-1.useRef๋ฅผ ํ™œ์šฉํ•œ Uncontrolled Component ๋ฐฉ์‹์œผ๋กœ input ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ

// signin.tsx
/* ... */
const emailRef = useRef<Input>(null); // null ํ• ๋‹น์„ ํ†ตํ•ด ํƒ€์ž… ์—๋Ÿฌ ๋ฐฉ์ง€
/* ... */
<Input ref={emailRef} />
/* ... */

6-2. ๋กœ๊ทธ์ธ API ํ˜ธ์ถœ ํ•จ์ˆ˜ ํƒ€์ž… ์ •์˜

children์„ ์ œ์™ธํ•˜๊ณ ๋Š” interface์—์„œ ์ •์˜ํ•œ ํƒ€์ž…๊ณผ Component์˜ props๊ฐ€ ๋™์ผํ•ด์ง‘๋‹ˆ๋‹ค.

// Signin.tsx (Component)
type LoginReqType = { // types.ts๋กœ ๋ถ„๋ฆฌํ•จ์œผ๋กœ์จ ์žฌ์‚ฌ์šฉํ•˜๊ฒŒ ํ•จ
  email: string;
  password: string;
};

interface SigninProps {
  login: (reqData: LoginReqType) => void;
}

const Signin: React.FC<SigninProps> = ({ login }) => {
  return <div></div>;
};
// SigninContainer.tsx (Container)
export default function SigninContainer() {
  const login = useCallback((reqData) => {
    /* saga ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋ถ€๋ถ„ */
  }, []);
  return <Signin login={login} />;
}

6-3. saga์—์„œ ๋น„๋™๊ธฐ ๋กœ์ง ๋ถ€๋ถ„์„ Service ๋ชจ๋“ˆ๋กœ ์œ„์ž„

.
โ””โ”€โ”€ src
    โ”œโ”€โ”€ redux
    โ”‚   โ”œโ”€โ”€ create.ts
    โ”‚   โ””โ”€โ”€ modules
    โ”‚       โ”œโ”€โ”€ reducer.ts
    โ”‚       โ”œโ”€โ”€ auth.ts
    โ”‚       โ””โ”€โ”€ rootSaga.ts
    โ”‚
    โ”œโ”€โ”€ services
    โ”‚   โ””โ”€โ”€ UserService.ts
    โ”‚
    โ””โ”€โ”€ types.ts
// services/UserService.ts

/* Token ๋ฐœ๊ธ‰์„ ์œ„ํ•œ API ๋กœ์ง ๋ถ„๋ฆฌ */
import axios from 'axios';
import { LoginReqType } from '../types';

const USER_API_URL = '';

/* API */
export default class UserService {
  public static async login(reqData: LoginReqType): Promise<string> {
    const response = await axios.post(USER_API_URL, reqData);
    return response.data.token;
  }
}

husky

npx husky install
yarn add lint-staged -D
npx husky add .husky/pre-commit 'lint-staged'

About

๐Ÿ“š Book Shelf Web Application in TypeScript, React and Redux.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 79.5%
  • CSS 15.1%
  • HTML 5.2%
  • Shell 0.2%