๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
STUDY/React

[39์ผ์ฐจ] ๋ฆฌ์•กํŠธ 200์ œ 6~10

by GWLEE 2022. 8. 16.

๐Ÿš˜2022-08-16๐Ÿš˜

 


006 ์ƒ๋ช…์ฃผ๊ธฐ ํ•จ์ˆ˜ static getDeriveState FormProps(props, state) ์‚ฌ์šฉํ•˜๊ธฐ

 

 

 

App.js

import React from 'react';
import './App.css';
import LifecycleEx from './R006_LifecycleEx'

function App() {
  return (
    <div>
      <h1>Start React 200!</h1>
      <p>CSS ์ ์šฉํ•˜๊ธฐ</p>
      <LifecycleEx
        prop_value = 'FromApp.js'
      />
    </div>
  );
}

export default App;

 

R006_LifecycleEx.js
import React, { Component } from 'react';

class R006_LifecycleEx extends Component {
    static getDerivedStateFromProps(props, state){
        console.log('2. getDerivedStateFromProps Call : '+props.prop_value);
        return {};
    }

    constructor(props){
        super(props);
        this.state = {};
        console.log('1. constructor Call');
    }

    render(){
        console.log('3. render Call');
        return(
            <h2>[THIS IS CONSTRUCTOR FUNCTION ]</h2>
        )
    }
}

export default R006_LifecycleEx;

 

 


007 ์ƒ๋ช…์ฃผ๊ธฐ ํ•จ์ˆ˜ componentDidMount

๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์‹คํ–‰ํ•จ.

 

 

 

 

App.js

import React from 'react';
import './App.css';
import LifecycleEx from './R007_LifecycleEx'   // R007_LifecycleEx.js ์ž„ํฌํŠธ

function App() {
  return (
    <div>
      <h1>Start React 200!</h1>
      <p>CSS ์ ์šฉํ•˜๊ธฐ</p>
      <LifecycleEx
        prop_value = 'FromApp.js'   // R006_LifecycleEx ์ปดํฌ๋„ŒํŠธ๋กœ prop_value ๋ผ๋Š” ๋ณ€์ˆ˜ ์ „๋‹ฌ
      />
    </div>
  );
}

export default App;

 

 

R007_LifecycleEx
 
// src/R007_LifecycleEx.js

import React, { Component } from 'react';

class R007_LifecycleEx extends Component {
  static getDerivedStateFromProps(props, state) {
    console.log('2. getDerivedStateFromProps Call :'+props.prop_value);
    return {tmp_state:props.prop_value};
  }

  constructor(props) {
    super(props);
    this.state = {};
    console.log('1. constructor Call');
  }

  componentDidMount() {
    console.log('4. componentDidMount Call');
    console.log('5. tmp_state : '+this.state.tmp_state);
  }

  render() {
    console.log('3. render Call');
    return (
      <h2>[ THIS IS COMPONENTDIDMOUNT FUCNTION ]</h2>
    )
  }
}

export default R007_LifecycleEx;

 

008 ์ƒ๋ช…์ฃผ๊ธฐ ํ•จ์ˆ˜ shouldComponentUpdate
 
 
 
App.js

 

import React from 'react';
import './App.css';
import LifecycleEx from './R008_LifecycleEx'   // R008_LifecycleEx.js ์ž„ํฌํŠธ

function App() {
  return (
    <div>
      <h1>Start React 200!</h1>
      <p>CSS ์ ์šฉํ•˜๊ธฐ</p>
      <LifecycleEx
        prop_value = 'FromApp.js'   
      />
    </div>
  );
}

export default App;
 
 
R008_LifecycleEx.js
 
// src/R007_LifecycleEx.js

import React, { Component } from 'react';

class R008_LifecycleEx extends Component {
  static getDerivedStateFromProps(props, state) {
    console.log('2. getDerivedStateFromProps Call :'+props.prop_value);
    return {tmp_state:props.prop_value};
  }

  constructor(props) {
    super(props);
    this.state = {};
    console.log('1. constructor Call');
  }

  componentDidMount() {
    console.log('4. componentDidMount Call');
    console.log('5. tmp_state : '+this.state.tmp_state);
    this.setState({tmp_state2 : true});
  }

  shouldComponentUpdate(props, state){
    console.log('6. shouldComponentUpdate Call / tmp_state2 = '
    + state.tmp_state2);
    return state.tmp_state2
  }

  render() {
    console.log('3. render Call');
    return (
      <h2>[ THIS IS COMPONENTDIDMOUNT FUCNTION ]</h2>
    )
  }
}

export default R008_LifecycleEx;

shouldComponentUpdate() ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์— ๋”ฐ๋ผ render() ํ•จ์ˆ˜๋ฅผ ์žฌ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ์ด์šฉํ•˜๋ฉด, props๋‚˜ state ๋ณ€์ˆ˜๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ํ™”๋ฉด์„ ๋‹ค์‹œ ๊ทธ๋ฆฌ๋ฉฐ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค. 


009 ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด ์‚ฌ์šฉํ•˜๊ธฐ

 

ES(ECMA ์Šคํฌ๋ฆฝํŠธ)๋Š” ํ‘œ์ค€ํ™”๋œ ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์ด๊ณ  ES ๋’ค์— ๋ถ™์€ ์ˆซ์ž๋Š” ๋ฒ„์ „์„ ์˜๋ฏธํ•จ.

2011๋…„ ์ดํ›„๋กœ ES5๊ฐ€ ์›นํ‘œ์ค€ ์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค. 2015๋…„ ๋ฐœํ–‰๋œ ES6๋Š” ๋งŽ์€ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋๊ณ 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์ด ๊ธฐ์ˆ  ๊ทœ๊ฒฉ์„ ๋”ฐ๋ฅธ๋‹ค. react๋„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ๋ฐ˜์˜ ์–ธ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ES6์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

R009_Es6.js
import React, { Component } from 'react';

class R009_Es6 extends Component {

  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    var jsString1 = ('์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ');
    var jsString2 = ('์ž…๋‹ˆ๋‹ค\n๋‹ค์Œ ์ค„์ž…๋‹ˆ๋‹ค.');
    console.log(jsString1+' ๋ฌธ์ž์—ด' + jsString2+'~');
    
    var Es6String1 = 'ES6'
    var Es6String2 = '์ž…๋‹ˆ๋‹ค'
    console.log(`${Es6String1} ๋ฌธ์ž์—ด${Es6String2}!!
    ________๋‹ค์Œ ์ค„์ž…๋‹ˆ๋‹ค`);

    var LongString = "ES6์— ์ถ”๊ฐ€๋œ String ํ•จ์ˆ˜๋“ค์ž…๋‹ˆ๋‹ค.";
    console.log('startsWith : ' + LongString.startsWith("ES6์— ์ถ”๊ฐ€"));
    console.log('endsWith : ' + LongString.endsWith("ํ•จ์ˆ˜๋“ค์ž…๋‹ˆ๋‹ค."));
    console.log('includes : ' + LongString.includes("์ถ”๊ฐ€๋œ String"));
  }

  render() {
    return (
      <h2>[ THIS IS ES6 STRING ]</h2>
    )
  }
}

export default R009_Es6;

 

R009_es6.js

import React from 'react';
import './App.css';
import LifecycleEx from './R009_Es6.js'   // R008_LifecycleEx.js ์ž„ํฌํŠธ

function App() {
  return (
    <div>
      <h1>Start React 200!</h1>
      <p>CSS ์ ์šฉํ•˜๊ธฐ</p>
      <LifecycleEx
        prop_value = 'FromApp.js'   
      />
    </div>
  );
}

export default App;

 

 


010 var, let, const ์‚ฌ์šฉํ•˜๊ธฐ

 

ES5์—์„œ ์‚ฌ์šฉํ•˜๋˜ var๋Š” ์œ ์—ฐํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ณ€์ˆ˜๋ฅผ ์žฌ์„ ์–ธ, ์žฌํ• ๋‹น ๊ฐ€๋Šฅ

-> ๋ณ€์ˆ˜์˜ ์‚ฌ์šฉ๋ฒ”์œ„๊ฐ€ ๋ถˆํ™•์‹ค, ์˜๋„ํ•˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๊ฐ’ ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ•จ.

-> ES6์—์„œ๋Š” let, const ์ถ”๊ฐ€

 

R010_Variable.js

import React, { Component } from 'react';

class R010_Variable extends Component {

  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    var varName = 'react';
    console.log('varName1 : '+varName);
    var varName = '200'; // 'varName' is already defined no-redeclare
    console.log('varName2 : '+varName);
    
    let letName = 'react'
    console.log('letName1 : '+letName);
    // let letName = '200'
    // parsing error : Identifier 'letName' has already been declared
    letName = 'react200';
    console.log('letName2 : ' + letName );

    const constName = 'react';
    console.log('constName : ' + constName );
    //const constName = '200'
    // Parsing error: Identifier 'constName' has already been declared
    // constName = 'react200'
    // Uncaught TypeError : Assignment to constant variable.
}

  render() {
    return (
      <h2>[ THIS IS Variable ]</h2>
    )
  }
}

export default R010_Variable;
 
 
R010_Variable.js
import React from 'react';
import './App.css';
import LifecycleEx from './R010_Variable.js'  

function App() {
  return (
    <div>
      <h1>Start React 200!</h1>
      <p>CSS ์ ์šฉํ•˜๊ธฐ</p>
      <LifecycleEx
        prop_value = 'FromApp.js'   
      />
    </div>
  );
}

export default App;

 


 

'STUDY > React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

React project..  (0) 2023.04.23
React  (0) 2023.04.16
1. react  (0) 2023.01.16
[37์ผ์ฐจ] ๋ฆฌ์•กํŠธ 200์ œ  (0) 2022.08.11

๋Œ“๊ธ€