๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“— React

[React] setState and Lifecycle

by Tamii 2021. 3. 16.
๋ฐ˜์‘ํ˜•

function component

//function component
function App() {
	return (   ) ;
}

return ํ•œ ๊ฐ’์„ screen ์— ํ‘œ์‹œ

 

class component

React class component ์—์„œ ๊ฐ€์ ธ์˜จ APP component

//App์€ react component
class App extends React.Component{
	
    render( 
    	return ()
    ) ;

}

React component์—์„œ ํ™•์žฅํ•œ ๊ฒƒ๋“ค์„ screen์— ํ‘œ์‹œ

react๊ฐ€  render() ๋ฉ”์„œ๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์‹คํ–‰ 

 

class component๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œต

state : object ,data๋ฅผ ๋„ฃ์„ ๊ณต๊ฐ„์ด ์žˆ์Œ 

 

data๋Š” ํ•ญ์ƒ ๋ณ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— state์™€ ๊ฐ™์€ ๊ณต๊ฐ„ ์ด ํ•„์š”

state(์ƒํƒœ)๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค React ๊ฐ€ render๋ฅผ ๋‹ค์‹œ ํ•ด์•ผ ํ•จ 

 

 


 

 

setState

state๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด 

state refresh   ->  render function ์‹คํ–‰

state ์™€  ํ•จ๊ผ render function ์„ ๊ณ„์† ์žฌํ˜ธ์ถœํ•จ 

 

 

render()  ๋ฉ”์„œ๋“œ

React componet๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฉ”์„œ๋“œ 

react๋Š” render๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ”๋กœ ์‹คํ–‰์‹œํ‚ด

 

<์˜ˆ์‹œ์ฝ”๋“œ >

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

  add = ()=>{

    this.setState({count: this.state.count+1 });
  };

  minus = ()=>{

    this.setState({count:this.state.count- 1 })

  };
  render(){
    return (
      <div>
        <h1> The number is: {this.state.count} </h1>
        <button onClick = {this.add}> Add</button>
        <button onClick={this.minus}>minus</button>
      </div>

    );
  }
}

 

state ๋ณ€๊ฒฝ์— ๋”ฐ๋ผ render ํ•˜๋Š” ๋™์ž‘ ๊ฐ€๋Šฅ 

ํ•˜์ง€๋งŒ ์ด๊ฑด ์ข‹์ง€ ์•Š์€ ์ฝ”๋“œ -> state์— ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ (this.state.count)

 

 

โญ๏ธcurrent ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ 

  add = ()=>{

    this.setState(current =>({count: current.count+1 }));
  };

  minus = ()=>{

    this.setState(current=>({count: current.count- 1 }))

  };

 

 

setSatate ํ˜ธ์ถœ  -> state ๊ฐ’ ๊ฐฑ์‹   -> render function ์‹คํ–‰ 

 

 


render ์™ธ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜  (์ค‘์š”ํ•˜์ง€ ์•Š์€๊ฒƒ๋“ค ์ฒ˜๋ฆฌ) 

Mounting()

component ํ‘œ์‹œ

 - constructor()

 - getDerivedStateFromProps()

 - render()

 - componentDidMount()

 

component๊ฐ€ mount -> screen ํ‘œ์‹œ -> constructor -> render -> componentDidMount()

 

 

Updating()

componentr๊ฐ€ ๊ฐœ๋ฐœ์ž์— ์˜ํ•ด state๊ฐ€ ๋ณ€๊ฒฝ 

 - getDerivedStateFromProps()

 - shouldComponentUpdate()

 - render()

 - getSnapshotBeforeUpdate()

 - componentdidUpdate()

 

Unmounting()

component์—†์• ๊ธฐ ( ํŽ˜์ด์ง€ ๋ณ€๊ฒฝ ์‹œ)

 - componentWillUnmount()

 

 

 componentDidMount(){
    console.log("component rendered")
  }
  componentDidUpdate(){
    console.log("I justupdate");
  }
  componentWillUnmount(){
    console.log("goodbye");
  }
  render(){
    console.log("i am rendering");
    return (
      <div>
        <h1> The number is: {this.state.count} </h1>
        <button onClick = {this.add}> Add</button>
        <button onClick={this.minus}>minus</button>
      </div>

    );
  }

์ด๋ ‡๊ฒŒ ํ˜ธ์ถœ๋˜์—ˆ์„ ๋•Œ 

 

i am rendering    ->   component rendered    ->  (๋ฒ„ํŠผ ํด๋ฆญ = state ๋ณ€๊ฒฝ)    -> i am renderging   ->   I justupdate

componentDidMount()   ->   componentDidUpdate()

์ˆœ์œผ๋กœ ๋™์ž‘ 

 


 

React์˜ LifeCycle  ๋ฉ”์„œ๋“œ 

React์˜ life cycle ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŠน์ • ์‹œ์ ์— ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ 

 

โ– componentDidMount()

์ฒ˜์Œ render ์‹œ ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ๋˜๋Š” life cycle ๋ฉ”์„œ๋“œ

 

class App extends React.Component{


  state={
    isLoading: true,
    movies:[]
  };
  
  
  componentDidMount(){
    setTimeout(() => {
      this.setState({isLoading:false})
    }, 6000);
  }
  
  
  render(){
    const {isLoading} = this.state;
    return (
      <div>
        {isLoading? "Loading...":"We are ready"}
      </div>

    );
  }

์ฝ”๋“œ์— ๋”ฐ๋ผ render ์‹œ

Mount() ์ง„ํ–‰ ( constructor() -> render() -> componentDidMount() )

๋™์ž‘์„ค๋ช…

 : isLoading์€ ํ˜„์žฌ true์ด๊ณ    ->  6์ดˆํ›„ isLodaing์˜ ๊ฐ’์— flase ์ง€์ •   ->   state๊ฐ€ refresh ๋˜๋ฉด์„œ ์žฌ render  

 

 

 

 

ko.reactjs.org/docs/react-component.html

 

React.Component – React

A JavaScript library for building user interfaces

ko.reactjs.org

 

๋Œ“๊ธ€