class Form extends TraditionalObjectOrientedView {
render() {
// Read some data passed to the view
const { isSubmitted, buttonText } = this.attrs;
if (!isSubmitted && !this.button) {
// Form is not yet submitted. Create the button!
this.button = new Button({
children: buttonText,
color: 'blue'
});
this.el.appendChild(this.button.el);
}
if (this.button) {
// The button is visible. Update its text!
this.button.attrs.children = buttonText;
this.button.render();
}
if (isSubmitted && this.button) {
// Form was submitted. Destroy the button!
this.el.removeChild(this.button.el);
this.button.destroy();
}
if (isSubmitted && !this.message) {
// Form was submitted. Show the success message!
this.message = new Message({ text: 'Success!' });
this.el.appendChild(this.message.el);
}
}
}
이를 리액트는 element 개념을 도입해 해결해줌
1) React.createElement() 함수
하나하나 타이핑해야하고 직관적이지 못해 가독성이 떨어짐 → JSX로 해결
React.createElement(
'div',
{ className: 'name' },
'React'
)
2) JSX 문법 사용하기
<div className='name'>React</div>
해당 태그를 가진 DOM 노드를 표현하며 React 가 실제로 화면에 렌더링 하는 대상
<div className='name'>React</div>
// element
{
type: '**div**',
props: {
className: 'name',
children: 'React'
}
}
props를 받아와 React Element를 반환하는 클래스 혹은 함수
애플리케이션에서 UI를 추상화, 모듈화
type이 string이 될때까지 계속(DOM node)를 찾을때까지 반복함
ReactDOM.render({
type: Form,
props: {
isSubmitted: false,
buttonText: 'OK!'
}
}, document.getElementById('root'));
{
type: SignUpForm
props: {
isSubmitted: false,
buttonText: 'OK!'
}
}
{
type: Button,
props: {
children: 'OK!',
color: 'blue'
}
}
type: 'button',
props: {
className: 'button button-blue',
children: {
type: 'b',
props: {
children: 'OK!'
}
}
}
}
엘리먼트들이 모여서 트리로 구성되고 가상 DOM에 대한 표현을 의미
this로 참조하는 것const myComponentInstance = new MyComponent();
https://hkc7180.medium.com/react-components-elements-and-instances-번역글-b5744930846b
https://seokzin.tistory.com/entry/React-엘리먼트와-컴포넌트-Element-Component-JSX