Tải bản đầy đủ (.docx) (2 trang)

Hướng dẫn ReactJS ReactJS tutorial (p4) Bài 4: ReactJS State trong React là gì?

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (54.86 KB, 2 trang )

Bài 4: ReactJS - State trong React
là gì?
State là nơi lưu trữ data trong ReactJs. Khi lập trình chúng ta nên đơn giản hóa state hết mức
có thể và hạn chế số lượng components chứa state. Ví dụ nếu chúng ta có 10 components
muốn có data từ state, chúng ta nên tạo một component và chứa state của 10 components kia.

Using Props
Các bạn xem ví dụ dưới đây về 1 component chứa state sử dụng EcmaScript2016 syntax.

App.js

import React from 'react';

class App extends React.Component {
constructor(props) {
super(props);

this.state = {
header: "Header from state...",
content: "Content from state..."
}
}
render() {
return (
<div>

{this.state.header}




{this.state.content}


</div>
);

}
}
export default App;

Index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';

ReactDOM.render(<App />, document.getElementById('app'));

Và ta được kết quả là:



×