All'alba vincerò

At dawn, I will win!

React/Coding Apple

class 컴포넌트

나디아 Nadia 2024. 3. 14. 19:11

 

 

class 컴포넌트

  • 옛날 문법
  • 요즘 잘 안씀
  • React에서 제공하는 Component라는 클래스를 extends (상속)

 

 

state 만들기

  • object 형태로 작성
class 작명 extends React.Component {
   constructor(props) {
	super(props);

	// class state 만들기
	this.state = {
		name : 'kim',
		age : 20
	}

	onIncrese = () => {
		const newCount = this.state.count + 1;
		this.setState({ count: newCount });
	};
   }
   
   render() {
	return (
	 <div>'안녕' {this.props}</div>
	 <button onClick={()=>{
		// state 변경하기
	 	this.setState({age : 21})
	  }} >버튼</button>
	)
   }
}

 

 

 

 

참고하기

 

[React] - Class 컴포넌트 vs Function 컴포넌트

React에서 컴포넌트를 Class와 Function으로 모두 만들 수 있다. Class는 React에서 제공하는 Component라는 클래스를 extends, 상속해서 만들 수 있다. Function은 간단하게 함수로 만들 수 있다. Class에는 상태,

velog.io

 

 

 


 

 

출처

코딩애플