whatisthis?
220317 TIL - markdown ver. ๋ณธ๋ฌธ
React.js
Today I Learned ... react.js
๐ ์์ - ๋๋ง์๊ธฐ ๊ฒ์
WordRelay.jsx
const React = require("react");
const { Component } = React;
class WordRelay extends Component {
state = {
word: "๊ฐ์์ง",
value: "",
result: "",
};
onSubmitForm = e => {
e.preventDefault();
if (this.state.value[0] === this.state.word[this.state.word.length - 1]) {
this.setState(prevState => ({
result: "์ ๋ต์
๋๋ค.",
value: "",
word: prevState.value,
}));
this.input.focus();
} else {
this.setState({
result: "ํ๋ ธ์ต๋๋ค.",
value: "",
});
this.input.focus();
}
};
onChangeInput = e => {
this.setState({
value: e.target.value,
});
};
input;
onRefInput = el => {
this.input = el;
};
render() {
return (
<>
<div>{this.state.word}</div>
<form onSubmit={this.onSubmitForm}>
<input
ref={this.onRefInput}
value={this.state.value}
onChange={this.onChangeInput}
/>
<button>์
๋ ฅ</button>
</form>
<div>{this.state.result}</div>
</>
);
}
}
module.exports = WordRelay;
Hooks ์ด์ฉ์
const React = require("react");
const { useState, useRef } = React;
const WordRelay = () => {
const [word, setWord] = useState("๊ฐ์์ง");
const [value, setValue] = useState("");
const [result, setResult] = useState("");
let inputRef = useRef(null);
const onSubmitForm = e => {
e.preventDefault();
if (value[0] === word[word.length - 1]) {
setResult("์ ๋ต์
๋๋ค");
setWord(value);
setValue("");
inputRef.current.focus();
} else {
setResult("ํ๋ ธ์ต๋๋ค");
setValue("");
inputRef.current.focus();
}
};
const onChangeInput = e => {
setValue(e.target.value);
};
return (
<>
<div>
{word}
<form onSubmit={onSubmitForm}>
<input onChange={onChangeInput} value={value} ref={inputRef} />
<button>์
๋ ฅ</button>
</form>
<div>{result}</div>
</div>
</>
);
};
module.exports = WordRelay;
๐ก target VS currentTarget
target | currentTarget |
---|---|
ํ๊ฒ | ํ์ฌ ์ด๋ฒคํธํธ๋ค๋ฌ๊ฐ ๋ถ์ฐฉ๋ ํ๊ฒ |
๐ก ์ฝ๋ฐฑ ref VS React.useRef
// 1๏ธโฃ Callback ref
<input ref={this.onRefInput} />
inputRef = null;
onRefInput = ( el => inputRef = el);
// 2๏ธโฃ React.useRef
<input ref={inputRef} />
const inputRef = React.useRef(null);
webpack build ์๋์ผ๋ก ํ๋ ๋ฐฉ๋ฒ
๋งค๋ฒ ์์ ํ ๋ ๋ง๋ค$ npx webpack
ํ๊ธฐ ๋ฒ๊ฑฐ๋ก์ฐ๋ฏ๋ก
webpack์ ์๋์ผ๋ก buildํ๋ ์ค์ ์ ํด์ฃผ์.
** webpack Dev-server / Hot-Reloading**
- npm i ๋ช ๋ น์ด๋ฅผ ํฐ๋ฏธ๋์ ์ ๋ ฅํด์ react-refresh์,
pmmmwh/react-refresh-webpack-plugin์ ์ค์นํด์ค๋ค.$ npm install react-refresh @pmmmwh/react-refresh-webpack-plugin -D
- ์ถ๊ฐ๋ก webpack Dev-server๋ ํ์ํ๋ค.
$ npm install -D webpack-dev-server
- package.json
package.json์ scripts ๋ถ๋ถ์ ์์ ํด์ค๋ค.
๋ณ๊ฒฝ ์ : "dev": "webpack"
๋ณ๊ฒฝ ํ : "dev": "webpack serve --env development
"scripts": {
"dev": "webpack serve --env development"
},
webpack.config.js
const RefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
// ์๋ต
module: {
...
},
plugins: [
new RefreshWebpackPlugin()
],
output: {
...
}
์ฐ์ , @pmmmwh ํ๋ฌ๊ทธ์ธ์ require๋ก ๋ถ๋ฌ์์ ๋ณ์๋ก ์ ์ฅํ ํ,plugins
์ new ๋ณ์๋ช
()์ ํด์ค๋ค.
module: {
rules: [
{
test: /\.jsx?/,
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: [
"@babel/plugin-proposal-class-properties",
"react-refresh/babel",
],
},
},
],
},
์ด๋ถ๋ถ๋ babel-loader์ ์ค์ ์ - plugins์ - "react-refresh/babel"
์ ์ถ๊ฐํ๋ค.
๐ dev-server ์ค์
output: {
path: path.join(__dirname, "dist"), //__dirname์ ํ์ฌํด๋๊ฒฝ๋ก
filename: "app.js",
publicPath: "/dist/",
},
devServer: {
publicPath: "/dist/",
hot: true,
},
devServer์ ์ค์ ์ ์์ ๊ฐ์ด
output์ publicPath๋ฅผ ๊ทธ๋๋ก ๊ฐ์ ธ์จ ํ, hot: true
๋ผ๊ณ ์ ์ด์ค๋ค.
๐ป ๊ณ์ dev-server ์ค์ ์ด ์ค๋ฅ๋์ ํ์ฐธ ํค๋งธ๋ค.
๋์ ์ฝ์ง ํ๊ธฐ๊ฐ ๊ถ๊ธํ๋ค๋ฉด ์๋ ํฌ์คํ
์ผ๋ก ๐
๋ฐ๋ ๋ถ๋ถ
output: {
path: path.join(__dirname, "dist"),
filename: "[name].js", // app.js์์ [name]์ผ๋ก
publicPath: "/dist",
},
devServer: {
devMiddleware: { publicPath: "/dist" },
static: { directory: path.resolve(__dirname) },
hot: true,
},
'WEB STUDY > REACT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Redux (๋ฆฌ๋์ค) - practice (0) | 2022.05.04 |
---|---|
React. styled-components (0) | 2022.05.01 |
220317 [TIL] Today I Learned ๐ฏ React.js (0) | 2022.03.17 |
220316 [TIL] Today I Learned ๐ฏ React.js (0) | 2022.03.16 |
220315 [TIL] Today I Learned ๐ฏ React.js - (2) (0) | 2022.03.15 |