๐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;
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;
// 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;
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;
// 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์ ๋ชจ๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋ค.
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;
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 |
๋๊ธ