Vite 환경변수 설정
가장 흔하게 리액트앱을 만드는 CRA (create-react-app)의 경우 .env 파일에 REACT_APP_을 prefix로 써야 리액트 앱이 인식할 수 있다. 그리고 앱에서 process.env.으로 접근하여 사용할 수 있었다.
Vite는 따로 dotenv 패키지를 설치해주지 않아도 되고, 공식 문서에 따르면 파일 prefix를 VITE_로 적어줘야 한다고 한다.
그리고 앱에서 import.meata.env.로 접근할 수 있다.
[React] vite에서 환경변수 .env 설정하기
환경변수는 번들러에 따라 다르게 설정된다는 사실 (프로젝트에 맞는 설정이 필요한 것이었다)알게된 계기는 프로젝트 실습이었다.(이래서 역시 이것저것 만들어봐야 하나 싶다)그리고 참고로,
velog.io
https://vitejs.dev/guide/env-and-mode.html
Vite
Next Generation Frontend Tooling
vitejs.dev
프로젝트 development, production 설정하기
Vite
Next Generation Frontend Tooling
vitejs.dev
msw 사용하기
backend api 배포가 되기 전에 개발하기 위해서 mock api를 설계해보자
// devdependency에 msw 추가
$ npm i -D msw
// public 디렉토리에 서비스 워커 코드 세팅하기
$ npx msw init public/ --save
// src/mocks/worker.js
import { setupWorker } from 'msw';
import { handlers } from './handlers';
export const worker = setupWorker(...handlers);
// /src/mocks/handlers.js
import { rest } from 'msw';
const todos = ['먹기', '자기', '놀기'];
export const handlers = [
// 할일 목록
rest.get('/todos', (req, res, ctx) => res(ctx.status(200), ctx.json(todos))),
// 할일 추가
rest.post('/todos', (req, res, ctx) => {
todos.push(req.body);
return res(ctx.status(201));
}),
];
function Home() {
axios.get('/todos').then((res) => {
console.log(res.data);
});
return <MainPageTemplate>Home</MainPageTemplate>;
}
MSW로 백앤드 API 모킹하기
Engineering Blog by Dale Seo
www.daleseo.com
eslint devdependency 제한 규칙 (msw)
'import/no-extraneous-dependencies': [
'error',
{ devDependencies: ['**/mocks/*'},
], // mock server devdependency 사용 제한 규칙 해제
msw를 사용할 mocks 디렉토리만 devDependency 사용 제한 규칙을 해제했다
GitHub - import-js/eslint-plugin-import: ESLint plugin with rules that help validate proper imports.
ESLint plugin with rules that help validate proper imports. - GitHub - import-js/eslint-plugin-import: ESLint plugin with rules that help validate proper imports.
github.com
API mocking
https://blog.mathpresso.com/msw%EB%A1%9C-api-%EB%AA%A8%ED%82%B9%ED%95%98%EA%B8%B0-2d8a803c3d5c
MSW로 API 모킹하기
프론트엔드 개발에서 가장 번거로운 작업 해결하는 방법
blog.mathpresso.com
Axios 설계하기
https://pinokio0702.tistory.com/373
[Axios][업무][베트남🇻🇳] - Axios instance 생성하고 api 요청 함수 작성하는 방법
안녕하세요. 회사에서 베트남 시니어 개발자 코드를 통해 학습한 내용을 일부 기록한 글입니다. axios를 잘 정리해서 사용한 것 같아서 따라서 사용하고 있습니다. 이 코드를 보고 개발하는 프로
pinokio0702.tistory.com
instance
https://yamoo9.github.io/axios/guide/config-defaults.html
Config 기본 설정 | Axios 러닝 가이드
Config 기본 설정 모든 요청에 적용될 구성 기본(Config Defaults) 값을 지정할 수 있습니다. 글로벌 axios 기본(defaults) 설정 axios.defaults.baseURL = 'https://api.example.com'; axios.defaults.headers.common['Authorization
yamoo9.github.io
interceptors
https://yamoo9.github.io/axios/guide/interceptors.html
인터셉터 | Axios 러닝 가이드
인터셉터 then이나catch로 처리되기 전에 요청이나 응답을 가로챌 수 있습니다. axios.interceptors.request.use( function (config) { return config; }, function (error) { return Promise.reject(error); }); axios.interceptors.response.us
yamoo9.github.io
Alert
https://sweetalert2.github.io/
SweetAlert2
A beautiful, responsive, customizable and accessible (WAI-ARIA) replacement for JavaScript's popup boxes
sweetalert2.github.io
https://mui.com/material-ui/react-alert/
React Alert component - Material UI
An alert displays a short, important message in a way that attracts the user's attention without interrupting the user's task.
mui.com
'개발일기' 카테고리의 다른 글
[TIL] 불편함을 못느꼈다면 그건 너무 잘 만든 것 (1) | 2022.12.27 |
---|---|
[TIL] 씁쓸한 마무리 (0) | 2022.12.22 |
[TIL] 준비 땅 (1) | 2022.12.17 |
[TIL] 이걸 몰라서 기획을 (0) | 2022.12.16 |
[TIL] 안되면 되게하라 (0) | 2022.12.15 |