0

I am using react csv for generating excel file.

I got a sample data downloaded but to download the JSON data, I am not sure how to pass the JSON files from RecipeReviewCardList file to tab-demo.js file.

Can you tell me how to pass the data providing my code changes and sandbox below ?

This will help me to learn more and fix the issues by myself in future. https://codesandbox.io/s/ko1q2x233

tab-demo.js

<div>
    230 sports | test Export Excel | Export PDF
    <CSVLink data={csvData}>Download me</CSVLink>;
    <CSVDownload data={csvData} target="_blank" />;
</div>

RecipeReviewCardList.js

 getCommentsData() {
    let comments = [];
    fetch("https://jsonplaceholder.typicode.com/comments")
      .then(response => response.json())
      .then(json => {
        comments = json;
        // comments = comments.slice(0,3);
        this.setState({ comments: comments });
        this.setState({ activeComments: comments.slice(0, 10) });
        //console.log(comments);

1 Answer 1

1

Code Sandbox (based on yours) with a solution: https://codesandbox.io/s/81v8p8kmy9

In your code is much bad "React styling", but according the question:

div section is moved from tab-demo.js to RecipeReviewCardList.js

    return this.state.comments.length > 0 ? (
      <div>
        <div>
          230 sports | test Export Excel | Export PDF
          <CSVLink data={this.state.csv}>Download me</CSVLink>;
          <CSVDownload data={this.state.csv} target="_blank" />;
        </div>
        {listView}
        <br />
        ...

function getCommentsData now look like this:

  getCommentsData() {
    let comments = [];
    let csv = [];
    let csvTitle = ["postId", "id", "name", "email", "body"];

    fetch("https://jsonplaceholder.typicode.com/comments")
      .then(response => response.json())
      .then(json => {
        csv = json.map(item => [
          item.postId,
          item.id,
          item.name,
          item.email,
          item.body
        ]);
        csv.unshift(csvTitle);

        comments = json;
        this.setState({ csv, comments, activeComments: comments.slice(0, 10) });
      });
  }

And, of course, csv is added to states.

I hope these will be helpful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.