๐ก Props ๋?
react ์์ props ๋ ์ ์ญ๋ณ์๋ก, component ๊ฐ์ ๊ฐ์ ์ ๋ฌํ๋ ์๋จ์ด๋ค. app.js (๋ถ๋ชจ ์ปดํฌ๋ํธ)์์ ๋ค๋ฅธ ์ปดํฌ๋ํธ(์์ ์ปดํฌ๋ํธ)๋ก ์ด๋ ํ ๊ฐ์ ์ ๋ฌํ ๋ props ๊ฐ์ฒด์ ๋ด์์ ๋ณด๋ด๊ฒ ๋๋ ๊ฒ์ด๋ค.
1. React props ์๋ฃํ ์ ์ธํ๊ธฐ
์์ ์ปดํฌ๋ํธ์์ props ์ ๋ํ ์๋ฃํ์ ์ ์ธํด๋์ผ๋ฉด, ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ๋์ด์ค๋ props ๋ณ์๋ค์ ์๋ฃํ๊ณผ ๋น๊ตํด์ ์๋ฃํ์ด ์ผ์นํ์ง ์์ผ๋ฉด ๊ฒฝ๊ณ ๋ฉ์ธ์ง๋ฅผ ์๋ ค์ฃผ๊ธฐ ๋๋ฌธ์ ์๋ชป๋ ๋ฐ์ดํฐ๋ฅผ ํ์ธํ ์ ์๋ค.
1. App.js ์์ ํ๊ธฐ
import './App.css';
import PropsDatatype from './R018_PropsDatatype';
function App() {
return (
<div>
<h1>Start React 200!</h1>
<p>CSS ์ ์ฉํ๊ธฐ</p>
<PropsDatatype
String="react"
Number={200}
Boolean={1==1}
Array={[0, 1, 8]}
ObjectJson={{react:"๋ฆฌ์กํธ", twohundred:"200"}}
Function={console.log("FunctionProps: function!")}
/>
</div>
);
}
export default App;
2. PropsDataType ํ์ผ ์์ฑํ๊ธฐ
import React, { Component } from 'react'
import datatype from 'prop-types';
class R018_PropsDatatype extends Component {
render() {
let {
String, Number, Boolean, Array, ObjectJson, Function
} = this.props;
return (
<div style={{padding: "0px"}}>
<p>StringProps: {String}</p>
<p>NumberProps: {Number}</p>
<p>BooleanProps: {Boolean.toString()}</p>
<p>ArrayProps: {Array.toString()}</p>
<p>Object JsonProps: {JSON.stringify(ObjectJson)}</p>
<p>FunctionProps: {Function}</p>
</div>
)
}
}
// props ์๋ฃํ ์ ์ธ
R018_PropsDatatype.propTypes = {
String: datatype.string,
Number: datatype.number,
Boolean: datatype.bool,
Array: datatype.array,
ObjectJson: datatype.object,
Function: datatype.func,
}
export default R018_PropsDatatype;
๋ง์ฝ props type ์ ์ธ๊ณผ ์ ๋ ฅ๋๋ props ํ์ ์ด ๋ค๋ฅผ ๊ฒฝ์ฐ์ warning ๋ฉ์ธ์ง๊ฐ ์ฝ์์ ๋ฌ๋ค.
2. React props ๊ฐ์ฒดํ์ผ๋ก ์ฌ์ฉํ๊ธฐ
1. App.js ์์ ํ๊ธฐ
import './App.css';
import PropsBoolean from './R019_PropsBoolean';
function App() {
return (
<div>
<h1>Start React 200!</h1>
<p>CSS ์ ์ฉํ๊ธฐ</p>
<PropsBoolean BooleanTrueFalse={false} />
<PropsBoolean BooleanTrueFalse />{/* default true */}
</div>
);
}
export default App;
2. Prop ํ์ผ ์์ฑํ๊ธฐ
import React, { Component } from 'react'
class R019_PropsBoolean extends Component {
render() {
let {
BooleanTrueFalse
} = this.props
return (
<div style={{padding: "0px"}}>
{BooleanTrueFalse ? '2. ' : '1. '}
{BooleanTrueFalse.toString()}
</div>
)
}
}
export default R019_PropsBoolean;
3. ์น ์คํ
๐ณ React ์์ ์๋ช ์ฃผ๊ธฐ๋?
Component์ ์์ฑ, ๋ณ๊ฒฝ, ์๋ฉธ์ ๊ณผ์ ์ ๋ปํ๋ค.
์ปดํฌ๋ํธ ์์ฑ ๊ณผ์ : render(), constructor(), getDerivedStateFormProps(), componentDidMount()
์ปดํฌ๋ํธ ๋ณ๊ฒฝ ๊ณผ์ : shouldComponentUpdate()
3. React ์๋ช ์ฃผ๊ธฐ ํจ์ render() ์ฌ์ฉํ๊ธฐ - ์์ฑ
1. App.js ์์ ํ๊ธฐ
import './App.css';
import React, { Component } from 'react';
function App() {
return (
<div>
<h1>Start React 200!</h1>
<p>CSS ์ ์ฉํ๊ธฐ</p>
<Bpp></Bpp>
</div>
);
}
class Bpp extends Component {
render() {
console.log('3. render call');
return (
<h2>[THIS IS RENDER FUNCTION]</h2>
);
}
}
export default App;
- render() ๋ return ๋๋ html ํ์์ ์ฝ๋๋ฅผ ํ๋ฉด์ ๊ทธ๋ ค์ฃผ๋ ํจ์์ด๋ค.
- ํ๋ฉด ๋ด์ฉ์ด ๋ณ๊ฒฝ๋์ด์ผ ํ ์์ ์ ์๋์ผ๋ก ํธ์ถ๋๋ค.
4. React ์๋ช ์ฃผ๊ธฐ ํจ์ constructor(props) ์ฌ์ฉํ๊ธฐ - ์์ฑ
1. App.js ์์ฑํ๊ธฐ
import './App.css';
import React, { Component } from 'react';
function App() {
return (
<div>
<h1>Start React 200!</h1>
<p>CSS ์ ์ฉํ๊ธฐ</p>
<Bpp tiger="ํธ๋์ด" lion="์ฌ์"></Bpp> {/* Bpp ์ปดํฌ๋ํธ๋ก tiger, lion ๋ณ์ ์ ๋ฌ */}
</div>
);
}
class Bpp extends Component {
constructor(props){ // ์๋ช
์ฃผ๊ธฐ ํจ์ ์ค ๊ฐ์ฅ ๋จผ์ ์คํ๋๋ค. ์ฒ์ ํ ๋ฒ๋ง ํธ์ถ๋๋ค.
super(props);
this.state={}; // ๋ณ์ props ๋ฅผ ์ด๊ธฐํํ ๋ ์ฌ์ฉ
console.log('1. Call'); // ์ฒซ ๋ฒ์งธ๋ก ์คํ
}
render() {
console.log(2, this.props.tiger); // ๋ ๋ฒ์งธ๋ก ์คํ
console.log(3, this.props.lion); // ์ธ ๋ฒ์งธ๋ก ์คํ
return (
<div></div>
);
}
}
export default App;
- constructor(props) ํจ์๋ ์๋ช ์ฃผ๊ธฐ ํจ์ ์ค ๊ฐ์ฅ ๋จผ์ ์คํ๋๊ณ , ์ฒ์ ํ ๋ฒ๋ง ํธ์ถ๋๋ค.
5. React ์๋ช ์ฃผ๊ธฐ ํจ์ static getDerivedStateFormProps(props, state) ์ฌ์ฉํ๊ธฐ - ์์ฑ
1. App.js ์์ฑํ๊ธฐ
import './App.css';
import React, { Component } from 'react';
function App() {
return (
<div>
<h1>Start React 200!</h1>
<p>CSS ์ ์ฉํ๊ธฐ</p>
<Bpp prop_value='FromApp.js'/>
</div>
);
}
class Bpp 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 RENDER FUNCTION]</h2>
);
}
}
export default App;
- App function ์์ prop_value ๋ฅผ Bpp ์ปดํฌ๋ํธ๋ก ์ ๋ฌํ๋ค.
- Bpp ์ปดํฌ๋ํธ์์ getDerivedStateFromProps() ํจ์๊ฐ constructor() ํจ์ ๋ค์์ผ๋ก ์คํ๋๋ค.
- ์ปดํฌ๋ํธ๊ฐ ์๋ก์ด props ๋ฅผ ๋ฐ๊ฒ ๋์์ ๋ state ๋ฅผ ๋ณ๊ฒฝํด์ค๋ค.
- props.prop_value ๋ก App function ์์ ์ ๋ฌํ prop_value ๋ณ์์ ์ ๊ทผํด์ ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
6. React ์๋ช ์ฃผ๊ธฐ ํจ์ componentDidMount() ์ฌ์ฉํ๊ธฐ - ์์ฑ
1. App.js ์์ฑํ๊ธฐ
import './App.css';
import React, { Component } from 'react';
function App() {
return (
<div>
<h1>Start React 200!</h1>
<p>CSS ์ ์ฉํ๊ธฐ</p>
<Bpp prop_value='FromApp.js'/>
</div>
);
}
class Bpp 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 FUNCTION]</h2>
);
}
}
export default App;
- componentDidMount() ํจ์๋ ์์ฑ ๋จ๊ณ์ ์๋ช ์ฃผ๊ธฐ ํจ์ ์ค (์์ฑํ ํจ์๋ค ์ค) ๊ฐ์ฅ ๋ง์ง๋ง์ผ๋ก ์คํ๋๋ค.
- render() ํจ์๊ฐ return ๋๋ html ํ์์ ์ฝ๋๋ฅผ ํ๋ฉด์ ๊ทธ๋ ค์ค ํ ์คํ๋๋ฉฐ, ํ๋ฉด์ด ๋ชจ๋ ๊ทธ๋ ค์ง ํ์ ์คํ๋์ด์ผ ํ๋ ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ ์ด๊ธฐํ ๋ฑ์ ๊ฐ์ฅ ๋ง์ด ํ์ฉ๋๋ ํจ์์ด๋ค.
7. React ์๋ช ์ฃผ๊ธฐ ํจ์ shouldComponentUpdate() ์ฌ์ฉํ๊ธฐ - ๋ณ๊ฒฝ
1. App.js ์์ฑํ๊ธฐ
import './App.css';
import React, { Component } from 'react';
function App() {
return (
<div>
<h1>Start React 200!</h1>
<p>CSS ์ ์ฉํ๊ธฐ</p>
<Bpp prop_value='FromApp.js'/>
</div>
);
}
class Bpp 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 shouldComponentUpdate FUNCTION]</h2>
);
}
}
export default App;
- props, state ์ ๋ณ๊ฒฝ์ด ๋ฐ์ํ๋ฉด ๋ณ๊ฒฝ ๋จ๊ณ์ ์๋ช ์ฃผ๊ธฐ ํจ์์ธ shouldComponentUpdate() ๊ฐ ์คํ๋๋ค.
- setState() ํจ์๋ก tmp_state2 ๋ณ์์ true ๋ผ๋ boolean ํ์ ์ ๋ฐ์ดํฐ๋ฅผ ์ธํ ํ๋ค.
- setState() ํจ์๋ ๋ณ์์ ์ ์ธ๊ณผ ์ด๊ธฐํ๋ฅผ ๋์์ ์คํํ๋ค.
- componentDidMount() ๊ฐ ์์ฑ ๋จ๊ณ์ ์๋ช ์ฃผ๊ธฐ ํจ์ ์ค ๊ฐ์ฅ ๋ง์ง๋ง์ผ๋ก ์คํ๋๋๋ฐ componentDidMount() ํจ์์์ tmp_state2 ๋ณ์์ state ๊ฐ ๋ณ๊ฒฝ๋์๊ธฐ ๋๋ฌธ์ ๊ทธ ๋ค์์ผ๋ก ๋ณ๊ฒฝ ๋จ๊ณ์ ์๋ช ์ฃผ๊ธฐ ํจ์์ธ shouldComponentUpdate() ๊ฐ ์คํ๋๋ค.
- shouldComponentUpdate() ๊ฐ ์คํ๋์ด์ boolean ์ ํ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๊ณ return ๊ฐ์ด true ์ผ ๋ render() ํจ์๋ฅผ ํ๋ฒ ๋ ํธ์ถํ๋ค.
- shouldComponentUpdate() ์ ๋ฐํ ๊ฐ์ ๋ฐ๋ผ render() ํจ์๋ฅผ ์ฌ์คํํ ์ ์๋ค. (๋ฆฌํด๊ฐ์ด false ์ผ ๊ฒฝ์ฐ render() ์ฌ์คํ์ด ๋์ง ์๋๋ค.)
*์ฐธ๊ณ : ๋ณธ ํฌ์คํ ์ ๋์ [์ด๋ณด์๋ฅผ ์ํ ๋ฆฌ์กํธ 200์ ] ๋ก ๊ณต๋ถํ๋ฉฐ ์ ๋ฆฌํ ๋ด์ฉ์ด๋ค.
'JavaScript > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] Redux ๋? (0) | 2021.10.21 |
---|---|
[React] React ์ด๋ฒคํธ ์ฌ์ฉํ๊ธฐ (0) | 2021.10.20 |
[React] React ์์ bootstrap ์ฌ์ฉํ๊ธฐ(ReactStrap) (0) | 2021.10.20 |
[React] HTTP ํต์ ์ ์ํ ๋ฐฉ๋ฒ (Fetch, Axios, Promise) (0) | 2021.10.20 |
[React] React.js ์์ํ๊ธฐ (0) | 2021.10.13 |
๋๊ธ