React.js - k821209/pipelines GitHub Wiki
starting react.js
# node.js ๊ฐ ์ค์น๋์ด์ผํจ
> node -v
# npm ์ด ์ค์น๋์ด์์๊ฒ
# npx ๋ฅผ ์ค์นํด์ผํจ
> npm install npx -g # global install๋ก npx๋ฅผ ์ค์นํจ
# ์ํ๋ ๋๋ ํ ๋ฆฌ๋ฅผ ๋ง๋ค๊ณ
> npx create-react-app {appName}
# VSC๋ก ์ด๊ธฐ
> code {appName}
# ๊ธฐ๋ณธ ํ์ผ ๊ด๋ฆฌ
# package.json
## scripts ์ test, eject๋ ์ญ์
# ํ
์คํธ ์คํ
> npm start
# git ๋ถ์ด๊ธฐ
> git init
> ๋ฑ๋ฑ
- ์ด๊ธฐ ์ ์ ์ ๋ค์๊ณผ ๊ฐ์ด
# index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
# app.js
import React from 'react';
function App() {
return (
<div className="App">
</div>
);
}
export default App;
# prop-types install : ์ปดํฌ๋ํธ๋ค์ props์ ๊ด๋ฆฌํด์ฃผ๋ ๋์ฐ๋ฏธ ํด... ์ฝ๋ฉ์ด ๋์กํด์ง๋ฉด ๊ผญ ์จ์ผํ ๋ฏ
npm i prop-types # ์ค์นํ๊ฒ ๋๋ฉด packages.json ์ด ์
๋ฐ์ดํธ ๋์ด ์๋ ๊ฒ์ ๋ณผ ์ ์์
# prop-types ํ์ฉ
import PropTypes from 'prop-types';
Food.propTypes = { # Food๋ ์ ์ ๋์ด ์๋ ํจ์(์ฝคํฌ๋ํธ) ์.
name = PropTypes.string.isRequired
age = PropTypes.string.isRequired # ์ด๋ฐ์์ผ๋ก Prop์ ๋ค์ด๊ฐ๋ ๋ณ์๋ค์ ์ฑ๊ฒฉ ๋ฐ ํ์ ์ฌ๋ถ๋ฅผ ๋ฏธ๋ฆฌ ์ง์ ํ ์ ์์
# Warning์ ๋ฟ๋๋ค.
}
- ๊ธฐ๋ณธ๊ฐ๋
# potato.js
import React from 'react';
function name(){ # component์ด๋ฆ์ด ๋จ ํฅํ ํด๋น ์ปดํฌ๋ํธ๋ html๋ฌธ๋ฒ์ด ๋ค์ด๊ฐ๋ ๊ณณ์์ <name /> ์์ผ๋ก ์ฝ์
์ด ๋๋ค.
return( # ์ฌ๋ฌ์ค์ด ๋ค์ด๊ฐ ๊ฒฝ์ฐ ๊ดํธ๋ฅผ ์ด์ด์ฃผ๊ณ , ํ์ค์ผ ๊ฒฝ์ฐ์๋ ๊ดํธ๊ฐ ์์ด๋ ๋๋ค.
<h1>Hello</h1># ์ฌ๊ธฐ์๋ HTML ๋ฌธ๋ฒ์ด ๋ค์ด์จ๋ค.
)
}
export default name; # App.js ์ธ์ ์ปดํฌ๋ํธ๊ฐ ์ ์๋์ด ์์ ๊ฒฝ์ฐ์ App.js์์ ๋ถ๋ฌ์ ์ฐ๊ธฐ ์ํด์๋ export๋ฅผ ํด์ค์ผํจ
# App.js
import React from 'react';
function Food(props){ # ์ฌ๊ธฐ์ props ๊ฐ json๋ชจ์์ ๋ฐ์ดํฐ๋ผ๋ ๊ฒ์ ์๊ณ ์๊ณ keys๋ฅผ ์๊ณ ์๊ณ ๊ทธ์ค์ ์ผ๋ถ๋ง ์ธ๋
# ์๋ฅผ ๋ค์ด fav ๋ณ์๋ง ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด props ๋์ ์ {fav} ๋ผ๊ณ ์
๋ ฅํด๋์ผ๋ฉด props ์ fav ์ value๋ง
# ์ฌ์ฉ๊ฐ๋ฅํ๋ค. return ์์ fav๋ก ๋ฐ๋ก ์ฌ์ฉ๊ฐ๋ฅ
# props ์๋ ๊ผญ key๋ฅผ ๋ง๋ค์ด์ค์ผ ์๋ฌ๊ฐ ์๋จ
return (
console.log(props)
<h1>hello {props.fav} </h1> # props ๋ฅผ htmlํํ ๋จ์์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ {} ๋ก ๋ณ์๋ช
์ ๋ฌถ์ด์ฃผ๋ฉด๋จ.
)
}
function App(){
return (
<div><Food fav='kimchi' boy={true} /></div> # Food component๋ค์์ ๋ค์ด๊ฐ๋ fav ๋ฑ๋ฑ์ property๋ผ๊ณ ๋ถ๋ฆ ์ค์ฌ์ props ๋ผ๊ณ ํจ. ์ด๊ฒ์ Food component์ ์ผ์ข
์ parameter๋ก ๋ค์ด๊ฐ๋ฉฐ json ํ์์ object๋ก ๋ค์ด๊ฐ๋๊ฒ์ console.log(props) ๋ก ํ์ธ ๊ฐ๋ฅ
)
{
- ๊ธฐ๋ณธ๋ฌธ๋ฒ
const certainArray = [1,2,3,4]
certainArray.map(function) # array ํ์์ ๋ฐ๋ก map ๋ฉ์๋๊ฐ ๋ค์ด์์. ๊ดํธ์์ ํ์ํ ํจ์๋ฅผ ๋ฃ์ผ๋ฉด ๊ทธ๋ง์.
# ๊ดํธ์์ function(){return calc} ๋ก ์งํํ๋ฉด return์ output ์ผ๋ก array๊ฐ ์์ฑ
# function ํํ์ ES6 ()=>{} ๊ฐ๋ฅ
# ngrok ์ค์น. ์ ๋ ๊ฒ ์ํ๋ฉด ์๋ฌ๋จ.
$ npm install -g ngrok --unsafe-perm=true --allow-root
$ ngrok http 3000 # port 3000์ธ ๊ฒฝ์ฐ
$ npx create-react-app weather
๋ฐฐํฌ
$ apt update
$ cd react_apps/
$ apt install git
$ git clone https://github.com/k821209/rnaseq_app.git
$ apt install -y npm nginx vim systemctl
$ npm install react-scripts
$ npm run build
$ rm /etc/nginx/sites-available/default
$ rm /etc/nginx/sites-enabled/default
$ cd /etc/nginx/sites-available/
$ touch app.conf
$ vi app.conf
$ ln -s /etc/nginx/sites-available/app.conf /etc/nginx/sites-enabled/app.conf
$ systemctl stop nginx
$ systemctl start nginx
# app.conf
server {
listen 8000;
location / {
root /react_apps/rnaseq_app/build;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}