๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ฌ/ใ…ใ……ใ…Œใ…‹ใ…ใ…… ์ฑŒ๋ฆฐ์ง€

37์ผ์ฐจ

๐Ÿง

37์ผ์ฐจ

 

Part 10. React ๋กœ ์‡ผํ•‘๋ชฐ ๋งŒ๋“ค๊ธฐ (React ๊ธฐ๋ณธ)

Ch 6. ๋ฆฌ์•กํŠธ ์‹ค์ „ ํ™œ์šฉ

 


 

 

Ch 6. ๋ฆฌ์•กํŠธ ์‹ค์ „ ํ™œ์šฉ

 

 

 ๐Ÿ”— HOC (Higher Order Component)  : <์ปดํฌ๋„ŒํŠธ> ๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ <์ƒˆ๋กœ์šด  ์ปดํฌ๋„ŒํŠธ> ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

 

https://ko.reactjs.org/docs/higher-order-components.html

 

๊ณ ์ฐจ ์ปดํฌ๋„ŒํŠธ – React

A JavaScript library for building user interfaces

ko.reactjs.org

https://github.com/mridgway/hoist-non-react-statics

 

GitHub - mridgway/hoist-non-react-statics: Copies non-react specific statics from a child component to a parent component

Copies non-react specific statics from a child component to a parent component - GitHub - mridgway/hoist-non-react-statics: Copies non-react specific statics from a child component to a parent comp...

github.com

 

HOC = function(์ปดํฌ๋„ŒํŠธ) { return ์ƒˆ๋กœ์šด ์ปดํฌ๋„ŒํŠธ; }

 

  • connect()
  • createFragmentContainer()
  • withRouter()

 

 

 ๐Ÿ”— Controlled Component ์™€ Uncontrolled Component 

 

 

  • Controlled Component

https://ko.reactjs.org/docs/forms.html#controlled-components

 

ํผ – React

A JavaScript library for building user interfaces

ko.reactjs.org

 

  • Uncontrolled Component

https://ko.reactjs.org/docs/uncontrolled-components.html

 

๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ – React

A JavaScript library for building user interfaces

ko.reactjs.org

 

// ./App.js

import logo from "./logo.svg";
import "./App.css";
import ControlledComponent from "./components/ControlledComponent";
import UncontrolledComponent from "./components/UncontrolledComponent";

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <ControlledComponent />
        <UncontrolledComponent />
      </header>
    </div>
  );
}

export default App;
// ./components/ControlledComponent.jsx

import React from "react";

class ControlledComponent extends React.Component {
  state = {
    value: "",
  };

  render() {
    const { value } = this.state;
    return (
      <div>
        <input value={value} onChange={this.change} />
        <button onClick={this.click}>์ „์†ก</button>
      </div>
    );
  }

  change = (e) => {
    console.log(e.target.value);
    this.setState({ value: e.target.value });
  };

  click = () => {
    console.log(this.state.value);
  };
}

export default ControlledComponent;
// UncotrolledComponents.jsx

import React from "react";

class UncontrolledComponent extends React.Component {
  inputRef = React.createRef();

  render() {
    console.log("initial render", this.inputRef);
    return (
      <div>
        <input ref={this.inputRef} />
        <button onClick={this.click}>์ „์†ก</button>
      </div>
    );
  }

  componentDidMount() {
    console.log("componentDidMount", this.inputRef);
  }

  click = () => {
    // input ์•จ๋ฆฌ๋จผํŠธ์˜ ํ˜„์žฌ ์ƒํƒœ ๊ฐ’(value) ์„ ๊บผ๋‚ด์„œ ์ „์†กํ•œ๋‹ค.
    // const input = document.querySelector("#my-input");
    // console.log(input.value);

    console.log(this.inputRef.current.value);
  };
}

export default UncontrolledComponent;

 

'๐Ÿ’ฌ > ใ…ใ……ใ…Œใ…‹ใ…ใ…… ์ฑŒ๋ฆฐ์ง€' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

39์ผ์ฐจ  (0) 2022.03.24
38์ผ์ฐจ  (0) 2022.03.23
36์ผ์ฐจ  (0) 2022.03.21
35์ผ์ฐจ  (0) 2022.03.21
34์ผ์ฐจ  (0) 2022.03.20