Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a component on clicking a button in React [duplicate]

I am new to react. How to render a component only after clicking a button in react?

Here in my case on clicking a button I have to display a table which displays the data from the database.

I have attached my code below for your reference, the first component is the button component and below that you can find the components for the table.

Also I would like to know how to refresh a component on clicking a button without refreshing the entire page.

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'))   
like image 946
Praveen Mohan Avatar asked Nov 21 '17 06:11

Praveen Mohan


People also ask

How do you show a component on clicking a button in React?

To show or hide another component on click in React: Set the onClick prop on an element. When the element is clicked, toggle a state variable that tracks if the component is shown. Conditionally render the component based on the state variable.

How do you capture double click in React?

To handle double click events in React: Add an onClick prop to the element. Use the detail property on the event object to get the click count. If the click count is equal to 2, handle the double click event.

How do you repeat components in React?

To repeat an element n times with React, we can use the Array function with the map array method. to create an array with 8 span elements with the same content by calling the Array with the n to create an empty array with n slots. Then we use the spread operator to make a copy of it.

How do I copy a div in react JS?

To duplicate a div onclick event with JavaScript, we can call cloneNode` to clone an element. Then we can set the onclick property of the cloned element to the same event handler function as the original element. to add a div. Then we deep clone the element by calling cloneNode with true .


2 Answers

I've set up a sandbox to showcase how you can do this.

In essence:

  1. Initialise state with a boolean set to false
  2. Render the component conditionally based on this boolean; so initially the component will now show up on the DOM
  3. On some action (onClick), setState on the boolean to true
  4. The component will re-render since the state changed and will now show the hidden component (since the boolean has been set to true)
like image 144
Raghudevan Shankar Avatar answered Sep 19 '22 12:09

Raghudevan Shankar


You can do something like this.

First Initialize the state with a property show with false when the component mounts.

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

Add a function to change the state. (You can use this function to toggle state as well)

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

Call the function on click of the button.

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

Add your table inside braces along with the expression like this.

{
  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>
}

Hope this helps!

like image 20
Edison D'souza Avatar answered Sep 17 '22 12:09

Edison D'souza