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

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

33์ผ์ฐจ

๐Ÿป

33์ผ์ฐจ

 

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

Ch 2. React Component

 


 

 

Ch 2. React Component

 

 

 ๐Ÿ”— React Component ๋งŒ๋“œ๋Š” ๋ฒ• 

 

// Class Component

// ์ •์˜
class ClassComponent extends React.Component {
    render() {
      return <div>Hello</div>;
    }
}

// ์‚ฌ์šฉ
ReactDOM.render(<ClassComponent />, document.querySelector("#root"));

 

// Function Component 1

// ์ •์˜
function FunctionComponent() {
	return <div>Hello</div>;
}

// ์‚ฌ์šฉ
ReactDOM.render(<FunctionComponent />, document.querySelector("#root"));
// Function Component 2

// ์ •์˜
const FunctionComponent = () => <div>Hello</div>;

// ์‚ฌ์šฉ
ReactDOM.render(<FunctionComponent />, document.querySelector("#root"));

 

 

 ๐Ÿ”— React.createElement ๋กœ ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ 

 

React.createElement(
	type, // ํƒœ๊ทธ ์ด๋ฆ„ ๋ฌธ์ž์—ด | ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ | React.Fragment
	[props], // ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์— ๋„ฃ์–ด์ฃผ๋Š” ๋ฐ์ดํ„ฐ ๊ฐ์ฒด
	[...children] // ์ž์‹์œผ๋กœ ๋„ฃ์–ด์ฃผ๋Š” ์š”์†Œ๋“ค
);

 

// 1. ํƒœ๊ทธ ์ด๋ฆ„ ๋ฌธ์ž์—ด type

// <h1>type ์ด "ํƒœ๊ทธ ์ด๋ฆ„ ๋ฌธ์ž์—ด" ์ž…๋‹ˆ๋‹ค.</h1>;
ReactDOM.render(
	React.createElement("h1", null, `type ์ด "ํƒœ๊ทธ ์ด๋ฆ„ ๋ฌธ์ž์—ด" ์ž…๋‹ˆ๋‹ค.`),
	document.querySelector("#root")
);
// 2. ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ type

const Component = () => {
    return React.createElement(
    	"p",
    	null,
    	`type ์ด "React ์ปดํฌ๋„ŒํŠธ" ์ž…๋‹ˆ๋‹ค.`
    );
};

// <Component /> => <p>`type ์ด "React ์ปดํฌ๋„ŒํŠธ" ์ž…๋‹ˆ๋‹ค.`</p>
ReactDOM.render(
	React.createElement(Component, null, null),
	document.querySelector("#root")
);
// 3. React.Fragment => ์š”์†Œ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

ReactDOM.render(
    React.createElement(
        React.Fragment,
        null,
        `type ์ด "React Fragment" ์ž…๋‹ˆ๋‹ค.`,
        `type ์ด "React Fragment" ์ž…๋‹ˆ๋‹ค.`,
        `type ์ด "React Fragment" ์ž…๋‹ˆ๋‹ค.`
    ),
    document.querySelector("#root")
);
  // 4. ๋ณต์žกํ•œ ๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ ๋ชจ์ž„

  // <div>
  //   <div>
  //     <h1>์ฃผ์ œ</h1>
  //     <ul>
  //       <li>React</li>
  //       <li>Vue</li>
  //     </ul>
  //   </div>
  // </div>;
  ReactDOM.render(
    React.createElement(
      "div",
      null,
      React.createElement(
        "div",
        null,
        React.createElement("h1", null, "์ฃผ์ œ"),
        React.createElement(
          "ul",
          null,
          React.createElement("li", null, "React"),
          React.createElement("li", null, "Vue")
        )
      )
    ),
    document.querySelector("#root")
  );

 

 

 ๐Ÿ”— JSX 

 

https://babeljs.io/

 

Babel · The compiler for next generation JavaScript

The compiler for next generation JavaScript

babeljs.io

<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

 

  // ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ์–ด๋–ค ์ฝ”๋“œ๋ฅผ => ์ˆœ์ˆ˜ํ•˜๊ฒŒ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
  // babel

  const title = "์ฃผ์ œ";
  ReactDOM.render(
    <div a="a">
      <div>
        <h1>{title}</h1>
        <ul>
          <li>React</li>
          <li>Vue</li>
        </ul>
      </div>
    </div>,
    document.querySelector("#root")
  );

 

 

 ๐Ÿ”— Props ์™€ State 

 

 

  // {message: '์•ˆ๋…•ํ•˜์„ธ์š”!'}
  function Component(props) {
    return (
      <div>
        <h1>{props.message} ์ด๊ฒƒ์€ ํ•จ์ˆ˜๋กœ ๋งŒ๋“  ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h1>
      </div>
    );
  }

  Component.defaultProps = {
    message: "๊ธฐ๋ณธ๊ฐ’",
  };
  
  ReactDOM.render(
    <Component message="๊ธฐ๋ณธ๊ฐ’ ์•„๋‹˜" />,
    document.querySelector("#root")
  );

 

  class Component extends React.Component {
    // state = {
    //   count: 0,
    // };

    constructor(props) {
      super(props);

      this.state = {
        count: 0,
      };
    }

    render() {
      return (
        <div>
          <h1>
            {this.props.message} ์ด๊ฒƒ์€ ํด๋ž˜์Šค๋กœ ๋งŒ๋“  ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.
          </h1>
          <p>{this.state.count}</p>
        </div>
      );
    }

    componentDidMount() {
      setTimeout(() => {
        // this.setState({
        //   count: this.state.count + 1,
        // });
        this.setState((previousState) => {
          const newState = {
            count: previousState.count + 1,
          };
          return newState;
        });
      }, 1000);
    }

    static defaultProps = {
      message: "๊ธฐ๋ณธ๊ฐ’2",
    };
  }

  ReactDOM.render(
    <Component message="๊ธฐ๋ณธ๊ฐ’ ์•„๋‹˜" />,
    document.querySelector("#root")
  );

 

 

 ๐Ÿ”— Event Handling 

 

  class Component extends React.Component {
    state = {
      count: 0,
    };
    // constructor(props) {
    //   super(props);

    //   this.click = this.click.bind(this);
    // }

    render() {
      return (
        <div>
          <p>{this.state.count}</p>
          <button onClick={this.click}>ํด๋ฆญ</button>
        </div>
      );
    }

    click = () => {
      console.log("clicked");
      this.setState((state) => ({
        ...state,
        count: state.count + 1,
      }));
    };
  }

  ReactDOM.render(<Component />, document.querySelector("#root"));

 

 

 ๐Ÿ”— Component Lifecycle 

 

(v16.3 ์ด์ „)

  class App extends React.Component {
    state = {
      age: 39,
    };
    interval = null;
    constructor(props) {
      super(props);
      console.log("constructor", props);
    }

    render() {
      console.log("render");
      return (
        <div>
          <h2>
            Hello {this.props.name} - {this.state.age}
          </h2>
        </div>
      );
    }

    componentWillMount() {
      console.log("componentWillMount");
    }
    componentDidMount() {
      console.log("componentDidMount");

      this.interval = setInterval(() => {
        // console.log("setInterval");
        this.setState((state) => ({ ...state, age: state.age + 1 }));
      }, 1000);
    }

    componentWillReceiveProps(nextProps) {
      console.log("componentWillReceiveProps", nextProps);
    }
    shouldComponentUpdate(nextProps, nextState) {
      console.log("shouldComponentUpdate", nextProps, nextState);

      return true; // โญ false or true
    }
    componentWillUpdate(nextProps, nextState) {
      console.log("componentWillUpdate", nextProps, nextState);
    }
    componentDidUpdate(prevProps, prevState) {
      console.log("componentDidUpdate", prevProps, prevState);
    }

    componentWillUnmount() {
      clearInterval(this.interval);
    }
  }

  ReactDOM.render(<App name="Mark" />, document.querySelector("#root"));

~๋ง๊ณ  ~์จ๋ผ ๋ผ๋Š” ๊ฒฝ๊ณ ๊ฐ€ ๋งŽ์ด ๋‚˜์˜ด

 

(v16.3)

  let i = 0;
  class App extends React.Component {
    state = {
      list: [],
    };

    render() {
      return (
        <div id="list" style={{ height: 100, overflow: "scroll" }}>
          {this.state.list.map((i) => {
            return <div>{i}</div>;
          })}
        </div>
      );
    }

    componentDidMount() {
      setInterval(() => {
        this.setState((state) => ({ list: [...state.list, i++] }));
      }, 1000);
    }

    getSnapshotBeforeUpdate(prevProps, prevState) {
      if (prevState.list.length === this.state.list.length) return null;
      const list = document.querySelector("#list");

      return list.scrollHeight - list.scrollTop;
    }

    componentDidUpdate(prevProps, prevState, snapshot) {
      console.log(snapshot);
      if (snapshot === null) return;
      const list = document.querySelector("#list");
      list.scrollTop = list.scrollHeight - snapshot;
    }
  }

  ReactDOM.render(<App name="Mark" />, document.querySelector("#root"));

 

 

 ๐Ÿ”— Component ์—๋Ÿฌ ์บ์น˜ - componentDidCatch 

 

https://reactjs.org/docs/error-boundaries.html

 

Error Boundaries – React

A JavaScript library for building user interfaces

reactjs.org

 

  class App extends React.Component {
    state = {
      hasError: false,
    };

    render() {
      if (this.state.hasError) {
        return <div>์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.</div>;
      }
      return <WebService />;
    }

    componentDidCatch(error, info) {
      this.setState({ hasError: true });
    }
  }

  ReactDOM.render(<App name="Mark" />, document.querySelector("#root"));

 

 

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

35์ผ์ฐจ  (0) 2022.03.21
34์ผ์ฐจ  (0) 2022.03.20
32์ผ์ฐจ  (0) 2022.03.18
31์ผ์ฐจ  (0) 2022.03.18
30์ผ์ฐจ  (0) 2022.03.18