Create React App

CRA

# npx create-react-app [폴더 명]
npx create-react-app tic-tac-toe
{
	"name": "tic-tac-toe",
	"version": "0.1.0",
	"private": true,
	"dependencies": {
		"@testing-library/jest-dom": "^5.16.5",
		"@testing-library/react": "^13.4.0",
		"@testing-library/user-event": "^13.5.0",
		"react": "^18.2.0",
		"react-dom": "^18.2.0",
		"react-scripts": "5.0.1",
		"web-vitals": "^2.1.4"
	},
	"scripts": {
		"start": "react-scripts start",
		"build": "react-scripts build",
		"test": "react-scripts test",
		"eject": "react-scripts eject"
	},
	"eslintConfig": {
	"extends": [
		"react-app",
		"react-app/jest"
	]},
	"browserslist": {
	"production": [
		">0.2%",
		"not dead",
		"not op_mini all"
	],
	"development": [
		"last 1 chrome version",
		"last 1 firefox version",
		"last 1 safari version"
		]
	}
}

package.json

npm start

개발 모드에서 띄울 수 있는 명령어
Screenshot 2023-06-27 at 6.19.06 AM.png

npm run build

Screenshot 2023-06-27 at 6.20.38 AM.png

npm install serve -g
serve -s build

npm test

!Screenshot 2023-06-27 at 6.11.06 AM.png

npm run eject

React를 화면에서 볼 수 있는 원리

Screenshot 2023-06-27 at 6.24.58 AM.png

ESLint

ESLint

mkdir eslint-test
cd eslint-test
npm init -y
npm install eslint -D
# eslint의 초기화
npx eslint --init

Screenshot 2023-06-27 at 6.32.45 AM.png

Screenshot 2023-06-27 at 6.32.05 AM.png

Screenshot 2023-06-27 at 6.33.27 AM.png

Screenshot 2023-06-27 at 6.34.08 AM.png

Screenshot 2023-06-27 at 6.34.34 AM.png

module.exprots = {
	"env" : {
		"browser" : true,
		"es2021" : true
	},
	"extends": "eslint:recommended",
	"parserOptions": {
		"ecmaVersion" : 12
	},
	"rules":{
		"semi" : ["error", "always"] // 세미콜론을 찍지 않으면, 에러를 발생시키겠다.
	}
}

Prettier

Prettier

Perttier 예시

mkdir prettier-test
cd prettier-test
npm init -y
npm install prettier -D
console.log('Hello')

index.js

npx prettier index.js
# console.log("Hello");

npx prettier index.js --write
{
	"singleQuote": true
}

Screenshot 2023-06-27 at 8.48.18 PM.png

husky

husky 설치

mkdir husky-test
cd husky-test
npm init -y

# husky는 git이 설치되어 있지 않은 환경에서 husky를 셋팅할 수 없다.
git init
npm install husky -D

# 프로젝트에서 husky를 활성화 하는 방법
npx husky install

# 만약 git을 commit 하기 직전에, package.json script에 test라는 코드를 실행하고 싶다면,
npx husky add .husky/pre-commit "npm test"
# 커밋을 하기 전에 무언가 규제를 하는 방법 중 하나다.

lint-staged

...
"script" :{
	"prepare" : "husky install",
	"start" : "react-scripts start",
	...

}
npx husky add .husky/pre-commit "npx lint-staged"
npm install lint-stage -D
...
	"lint-stage":{
		"**/*.js":[
			"eslint --fix",
			"prettier --write",
			"git add"
		]
	}

image src 경로 설정 방법

  • ./../public/image.png 해당 방식으로 src 경로를 설정해도 되고
  • import src from './image.png' 로 한뒤 src = {src} 로 설정할 수 있다.
    • 상대졍로로 설정한 경우, public 폴더 밑에 위치해 있어야한다.

Switch / NotFound

Switch

Switch?

  • 여러 Route 중 순서대로 먼저 맞는 하나만 보여줍니다.
  • exact를 뺄 수 있는 로직을 만들 수 있습니다.
  • 가장 마지막에 어디 path 에도 맞지 않으면, 보여지는 컴포넌트를 설정해서, Not Found 페이지를 만들 수 있습니다.

import { BrowserRouter, Route, Switch } from "react-router-dom";
import Home from "./pages/Home";
import Profile from "./pages/Profile";
import About from "./pages/About";

function App() {
  return (
    <BrowserRouter>
      <Switch>
      // 가장 넓은 범위를 가지고 있는 것을 아래로 내려야한다.
        <Route path="/profile/:id" component={Profile} />
        <Route path="/profile" component={Profile} />
        <Route path="/about" component={About} />
        <Route path="/" component={Home} />
        <Route component={NotFound} />
      </Switch>
    </BrowserRouter>
  );
}

export default App;

App.js

export default function NotFound(){
	return <div>페이지를 찾을 수 없습니다.</div>;
}

NotFound.jsx

JSX 링크로 라우팅 이동하기

<a href="/">Home</a>
import {Link} from 'react-router-dom';

...

<Link to="/">Home</Link>

예시

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

export default function Links(){
	return(<ul>
		<li>
			<Link to="/">Home</Link>
		</li>
		<li>
			<Link to="/profile">Profile</Link>
		</li>
		<li>
			<Link to="/profile/1">Profile/1</Link>
		</li>
		<li>
			<Link to="/aboud">Aboud</Link>
		</li>
		<li>
			<Link to="/about?name=mark">About?name=mark</Link>
		</li>
	</ul>);
}

src/component/Links.jsx

! 300

import {NavLink} from 'react-router-dom'

예시

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

const activeStyle = { color: "green"}

export default function NavLinks(){
	return(<ul>
		<li>
			<NavLink to="/" exact activeStyle={activeStyle}>Home</NavLink>
		</li>
		<li>
			<NavLink to="/profile" exact activeStyle={activeStyle}>Profile</NavLink>
		</li>
		<li>
			<NavLink to="/profile/1" exact activeStyle={activeStyle}>Profile/1</NavLink>
		</li>
		<li>
			<NavLink 
				to="/aboud" 
				exact 
				activeStyle={activeStyle} 
				isActive={(match, location) =>{
					console.log(location);
					// pathname = about
					// search = ?name=mark
					return location.search === '';
				}}
			>Aboud</NavLink>
		</li>
		<li>
			<NavLink 
				to="/about?name=mark" 
				exact activeStyle={activeStyle}
				isActive = {(match, localtion) =>{
					return localion.search === ' ?name=mark'
				}}
>			About?name=mark</NavLink>
		</li>
	</ul>);
}

src/component/Links.jsx


#React #FrontEnd