Pra*_*han 3 javascript asp.net-mvc reactjs
我是新来的反应.如何在单击反应按钮后才渲染组件?
在我的情况下,单击一个按钮,我必须显示一个表格,显示数据库中的数据.
我在下面附上了我的代码供您参考,第一个组件是按钮组件,下面是您可以找到该组件的组件.
此外,我想知道如何刷新组件单击按钮而不刷新整个页面.
var Button = React.createClass({
render: function () {
return (
<button type="button">Display</button>
); }
});
var EmployeeRow = React.createClass({
render: function () {
return (
<tr>
<td>{this.props.item.EmployeeID}</td>
<td>{this.props.item.FirstName}</td>
<td>{this.props.item.LastName}</td>
<td>{this.props.item.Gender}</td>
</tr>
);
}
});
var EmployeeTable = React.createClass({
getInitialState: function(){
return{
result:[]
}
},
componentWillMount: function(){
var xhr = new XMLHttpRequest();
xhr.open('get', this.props.url, true);
xhr.onload = function () {
var response = JSON.parse(xhr.responseText);
this.setState({ result: response });
}.bind(this);
xhr.send();
},
render: function(){
var rows = [];
this.state.result.forEach(function (item) {
rows.push(<EmployeeRow key={item.EmployeeID} item={item} />);
});
return (
<Button />
<table className="table">
<thead>
<tr>
<th>EmployeeID</th>
<th>FirstName</th>
<th>LastName</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
{rows}
</tbody>
</table>
);
} });
ReactDOM.render(<EmployeeTable url="api/Employee/GetEmployeeList" />,
document.getElementById('grid'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15380 次 |
| 最近记录: |