whatisthis?

220317 TIL - markdown ver. ๋ณธ๋ฌธ

WEB STUDY/REACT

220317 TIL - markdown ver.

thisisyjin 2022. 3. 17. 16:20

React.js

Today I Learned ... react.js

๐Ÿ™‹โ€โ™‚๏ธReact.js Lecture

๐Ÿ™‹โ€My Dev Blog


๐Ÿš€ ์˜ˆ์ œ - ๋๋ง์ž‡๊ธฐ ๊ฒŒ์ž„

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**

  1. npm i ๋ช…๋ น์–ด๋ฅผ ํ„ฐ๋ฏธ๋„์— ์ž…๋ ฅํ•ด์„œ react-refresh์™€,

pmmmwh/react-refresh-webpack-plugin์„ ์„ค์น˜ํ•ด์ค€๋‹ค.
$ npm install react-refresh @pmmmwh/react-refresh-webpack-plugin -D

  1. ์ถ”๊ฐ€๋กœ 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 ์„ค์ •์ด ์˜ค๋ฅ˜๋‚˜์„œ ํ•œ์ฐธ ํ—ค๋งธ๋‹ค.
๋‚˜์˜ ์‚ฝ์งˆ ํ›„๊ธฐ๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์•„๋ž˜ ํฌ์ŠคํŒ…์œผ๋กœ ๐Ÿ˜‚

๐Ÿ˜ฅ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