【发布时间】:2018-05-04 13:12:52
【问题描述】:
我是新来的反应。如何仅在单击反应中的按钮后才呈现组件?
在我的情况下,单击按钮时,我必须显示一个表格,该表格显示数据库中的数据。
我在下面附上了我的代码供您参考,第一个组件是按钮组件,您可以在下面找到表格的组件。
我还想知道如何在不刷新整个页面的情况下单击按钮刷新组件。
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'))
【问题讨论】:
标签: javascript asp.net-mvc reactjs