React์์ ๊ธฐ์กด์ ์ฌ์ฉํ๋ Class๋ฅผ ์ด์ฉํ ์ฝ๋๋ฅผ ์์ฑํ ํ์ ์์ด,
state์ ์ฌ๋ฌ React ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋๋ก ๋ง๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
useState๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ Hook๋ก, ํจ์ํ ์ปดํฌ๋ํธ์์๋ ๊ฐ๋ณ์ ์ธ ์ํ๋ฅผ ์ง๋๊ณ ์์ ์ ์๊ฒ ํด์ค๋ค. ๋ง์ฝ์ ํจ์ํ ์ปดํฌ๋ํธ์์ ์ํ๋ฅผ ๊ด๋ฆฌํด์ผ ๋๋ ์ผ์ด ๋ฐ์ํ๋ค๋ฉด ์ด Hook์ ์ฌ์ฉํ๋ฉด ๋๋ค.
import React, { useState } from 'react';
function Example() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
useEffect๋ ๋ฆฌ์กํธ ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง ๋ ๋๋ง๋ค ํน์ ์์ ์ ์ํํ๋๋ก ์ค์ ํ ์ ์๋ Hook์ด๋ค.
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
useContext๋ ํจ์ํ ์ปดํฌ๋ํธ์์ Context๋ฅผ ๋ณด๋ค ๋ ์ฝ๊ฒ ์ฌ์ฉ ํ ์ ์๋ค. useContext๋ฅผ ํธ์ถํ ์ปดํฌ๋ํธ๋ context ๊ฐ์ด ๋ณ๊ฒฝ๋๋ฉด ํญ์ ๋ฆฌ๋ ๋๋ง ๋๋ค.
const themes = {
light: {
foreground: "#000000",
background: "#eeeeee"
},
dark: {
foreground: "#ffffff",
background: "#222222"
}
};
const ThemeContext = React.createContext(themes.light);
function App() {
return (
<ThemeContext.Provider value={themes.dark}>
<Toolbar />
</ThemeContext.Provider>
);
}
function Toolbar(props) {
return (
<div>
<ThemedButton />
</div>
);
}
function ThemedButton() {
const theme = useContext(ThemeContext);
return (
<button style={{ background: theme.background, color: theme.foreground }}>
I am styled by theme context!
</button>
);
}
useRef๋ ํจ์ํ ์ปดํฌ๋ํธ์์ ref๋ฅผ ์ฝ๊ฒ ์ฌ์ฉ ํ ์ ์๊ฒ ํด์ค๋ค. .current ํ๋กํผํฐ๋ก ์ ๋ฌ๋ ์ธ์(initialValue)๋ก ์ด๊ธฐํ๋ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ref ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ฉฐ ๋ฐํ๋ ๊ฐ์ฒด๋ ์ปดํฌ๋ํธ์ ์ ์์ ์ฃผ๊ธฐ๋ฅผ ํตํด ์ ์ง๋๋ค.
function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// `current` points to the mounted text input element
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}
๊ณต์๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๋ฉด ์์ Hook ๋ด์ฉ ์ธ์ ๋ค๋ฅธ Hook ์ ๋ณด๋ฅผ ๋ณผ ์ ์๋ค. ๋ํ ์ง์ Hook์ ์ ์ํ๊ฑฐ๋ ๋ค๋ฅธ ์ฌ๋์ด ๋ง๋ Hook์ ๊ฐ์ ธ๋ค ์ฌ์ฉํ ์๋ ์๋ค.
Webpack์ ๋ชจ๋ JavaScript ์ ํ๋ฆฌ์ผ์ด์
์ ์ํ ์ ์ ๋ชจ๋ ๋ฒ๋ค๋ฌ ์ด๋ค. ๊ฐ๋จํ๊ฒ ํํํ์๋ฉด ์ฌ๋ฌ ํ์ผ์ ํ๋ ์ด์์ ํ์ผ๋ก ํฉ์ณ์ฃผ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๋ฒ๋ค๋ฌ๋ก, webpack.config.js
์์ ์์ฑ์ ์ค์ ํ ์ ์๋ค.
๋ชจ๋ ๋ฒ๋ค๋ฌ
์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ฑํ๋ ์์(HTML, CSS, Javscript, Images ๋ฑ)์ ๋ชจ๋ ๊ฐ๊ฐ์ ๋ชจ๋๋ก ๋ณด๊ณ ์ด๋ฅผ ์กฐํฉํด์ ๋ณํฉ๋ ํ๋์ ๊ฒฐ๊ณผ๋ฌผ์ ๋ง๋๋ ๋๊ตฌ๋ฅผ ์๋ฏธํ๋ค.
./src/index.js
์ด์ง๋ง, webpack ์ค์ ์์ entry
์์ฑ์ ์ค์ ํ์ฌ ๋ค๋ฅธ entry๋ฅผ ์ง์ ํ ์ ์๋ค.module.exports = {
entry: './path/to/my/entry/file.js',
};
./dist/main.js
๋ก, ์์ฑ๋ ๊ธฐํ ํ์ผ์ ๊ฒฝ์ฐ์๋ ./dist
ํด๋๋ก ์ค์ ๋๋ค.const path = require('path');
module.exports = {
entry: './path/to/my/entry/file.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-first-webpack.bundle.js',
},
};
์์ ์์ค์์ ๋ก๋๋ webpack ์ค์ ์ ๋ ๊ฐ์ง ์์ฑ์ ๊ฐ์ง๋ค.
- ๋ณํ์ด ํ์ํ ํ์ผ์ ์๋ณํ๋
test
์์ฑ- ๋ณํ์ ์ํํ๋๋ฐ ์ฌ์ฉ๋๋ ๋ก๋๋ฅผ ๊ฐ๋ฆฌํค๋
use
์์ฑ
const path = require('path');
module.exports = {
output: {
filename: 'my-first-webpack.bundle.js',
},
module: {
rules: [{ test: /\.txt$/, use: 'raw-loader' }],
},
};
require()
๋ฅผ ํตํด ํ๋ฌ๊ทธ์ธ์ ์์ฒญํ๊ณ plugins
๋ฐฐ์ด์ ์ถ๊ฐํด์ผ ํ๋ค. ๋๋ถ๋ถ์ ํ๋ฌ๊ทธ์ธ์ ์ต์
์ ํตํด ์ฌ์ฉ์๊ฐ ์ง์ ํ ์ ์๊ณ , ๋ค๋ฅธ ๋ชฉ์ ์ผ๋ก ํ๋ฌ๊ทธ์ธ์ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ๋๋ก ์ค์ ํ ์ ์์ผ๋ฏ๋ก new
์ฐ์ฐ์๋ก ํธ์ถํ์ฌ ํ๋ฌ๊ทธ์ธ์ ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค.const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack'); // ๋ด์ฅ plugin์ ์ ๊ทผํ๋ ๋ฐ ์ฌ์ฉ
module.exports = {
module: {
rules: [{ test: /\.txt$/, use: 'raw-loader' }],
},
plugins: [new HtmlWebpackPlugin({ template: './src/index.html' })],
};
mode
๋ฅผ development
, production
๋๋ none
์ผ๋ก ์ค์ ํ๋ฉด webpack์ ๋ด์ฅ๋ ํ๊ฒฝ๋ณ ์ต์ ํ๋ฅผ ํ์ฑํ ํ ์ ์๋ค.production
์ด๋ค.module.exports = {
mode: 'production',
};
module.exports = {
entry: {
'main': './src/index.js',
'polyfills': './src/polyfills/index.js',
'detect.polyfills': './src/polyfills/detect.js'
},
output: {
path: path.resolve(__dirname, './dist'),
filename: '[name].js'
},
// ...
}
์์ง webpack dev server๋ฅผ ํซ๋ก๋ฉํ ๋์ npm start
๋ก ๊ฐ๋ฐํ ๋์ ์ฐจ์ด์ ์ ์ ๋ชจ๋ฅด๊ฒ ๋ค. ๋์ด ์๋ก ์ฐ๊ด์ด ๋์ด์์ง ์์์ webpack์ ์ฌ์ฉํ๋ ค๋ฉด ํซ๋ก๋ฉ์ด ํ์์ธ์ง ๋ ์์๋ด์ผ ํ ๊ฒ ๊ฐ๋ค.