【问题标题】:Display a component on clicking a button in React [duplicate]在 React 中单击按钮时显示组件 [重复]
【发布时间】: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


【解决方案1】:

我已经设置了sandbox to showcase,您可以如何做到这一点。

本质上:

  1. 初始化状态,布尔值设置为false
  2. 根据此布尔值有条件地渲染组件;所以最初组件现在将显示在 DOM 上
  3. 在某些操作上 (onClick),setState 在布尔值上到 true
  4. 组件将在状态更改后重新渲染,现在将显示隐藏的组件(因为布尔值已设置为 true

【讨论】:

  • 感谢代码沙箱的链接,很有帮助
  • 非常精确的例子,谢谢。
【解决方案2】:

你可以这样做。

首先在组件挂载时使用属性 show 初始化状态,设置为 false。

componentDidMount() {
  this.state = {
    show: false
  };
}

添加一个函数来改变状态。 (您也可以使用此功能来切换状态)

showTable() {
  this.setState({
    show: true
  });
}

点击按钮调用函数。

<button onclick="showTable()">
  Show Table
</button>

在大括号内添加您的表格以及这样的表达式。

{
  this.state.show &&
  <table className="table">
     <thead>
         <tr>
            <th>EmployeeID</th>
            <th>FirstName</th>
            <th>LastName</th>
            <th>Gender</th>               
         </tr>
     </thead>
      <tbody>
          {rows}
      </tbody>
  </table>
}

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2020-09-14
    • 2020-05-09
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    相关资源
    最近更新 更多