securedeveloper / react-data-export

Export dataset in react.
https://securedeveloper.github.io/react-data-export/
MIT License
179 stars 194 forks source link

Fetch data before download #57

Closed bg87 closed 6 years ago

bg87 commented 6 years ago

My reason: app needs to get the data before downloading it

Steps to reproduce:

Lib Version:

Is there any way to keep react-data-export from exporting until after the app gets the data. In all of the examples the data is already available but I have a case where I need to fetch the data to be exported only when the user clicks the export button.

securedeveloper commented 6 years ago

a small example to help you.

class App extends React.Component {
    constructor(props){
       super(props);
      this.state = {data: null}; //initially you don't have any data
    }

   componentDidMount() {
      //Fetch data here or any other suitable lifecycle and fill the state data
    }

    render() {
      return (
       <div>
          {this.renderOtherStuff()} //Render what else you want to
          {this.renderExcelDownloadButton()}
      </div>
      );
    }

   renderOtherStuff() {
    //TODO: return anything you want here
   }

  renderExcelDownloadButton() {
    if(this.state.data !== null) {
        //here return excel download button.
    }
  }
}

Hopefully this will help, re-open the issue if you need more help. :)