Làm sao để tạo các component trong ReactJS?

{{FormatNumbertoThousand(model.total_like)}} lượt thích
1.775 lượt xem
Reactjs junior

Có hai cách:

1. Functional component: đây là cách đơn giản nhất để tạo các ReactJS component. Nó chấp nhận các object làm props và trả về các ReactJS element. Chúng tôi gọi nó là "functional" bởi vì đó là các hàm JavaScript thuần (pure Javascript).

function Greeting(props) {
   return <h1> Hello, {props.message}</h1>

}

2. Class component: bạn cũng có thể sử dụng ES6 class để định nghĩa component. Class component trên có thể được viết như dưới đây:

class Greeting extends React.Component {
   render() {
      return <h1>Hello, {this.props.message}</h1>;
   }
}
{{login.error}}