왜 사용해야 하는가?
동작 원리 및 예시 코드:
import React, { useState, useEffect } from "react";
const ExampleComponent = () => {
const [count, setCount] = useState(0);
useEffect(() => {
console.log("Component mounted or updated, count:", count);
}, [count]);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
};
export default ExampleComponent;
코드 설명
공식 문서:
Webpack 예시 설정 (webpack.config.js):
module.exports = {
entry: "./src/index.tsx",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
module: {
rules: [
{
test: /\\.(js|jsx|ts|tsx)$/,
exclude: /node_modules/,
use: "babel-loader",
},
],
},
devServer: {
port: 8080,
historyApiFallback: true,
},
};
Babel 예시 설정 (.babelrc):
{
"presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"]
}
코드 설명
공식 문서:
왜 사용해야 하는가?
동작 원리 및 예시 코드:
import express from "express";
import bodyParser from "body-parser";
const app = express();
const port = process.env.PORT || 3001;
app.use(bodyParser.json());
// API 라우트 예시
app.get("/api/hello", (req, res) => {
res.json({ message: "Hello, world!" });
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});