Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Table if Array is empty, no render

Tags:

reactjs

I have 4 arrays which get data from an API, then i render 4 tables with those arrays, the problem here is, if one of those arrays is empty ( no data on the backend ) all the tables wont render..

completedIssues: []
issuesNotCompletedInCurrentSprint: []
puntedIssues: []
issuesCompletedInAnotherSprint: []


const filteredIssuesCompletedInAnotherSprint = this.state.issuesCompletedInAnotherSprint.map(item => (
        {
            assignee: item ? item.assigneeName : 'Empty',
            id: item ? item.id : 'Empty',
            key: item ? item.key : 'Empty',
            type: item ? item.typeName : 'Empty',
            summary: item ? item.summary : 'Empty',
        }
    ));


<ResponseTable data={filteredCompletedIssues} />
<ResponseTable data={filteredIssuesNotCompletedInCurrentSprint} />
<ResponseTable data={filteredPuntedIssues} />
<ResponseTable data={filteredIssuesCompletedInAnotherSprint} />

ResponseTable It receives an array of objects and render the data into the table...

import React from 'react';
import ReactTable from 'react-table';
import "react-table/react-table.css";
 
export default class ResponseTable extends React.Component {

    constructor(props) { // Use Props
        super(props); // Use Props
        this.columnsBuilder = this.columnsBuilder.bind(this);

    }
    columnsBuilder () { //Remove data
        if(this.props.data == 0){
           return
        }
        const props = Object.keys(this.props.data[0]); //Use Props
        const columns = props.map( (item, index) => ({
            Header : item,
            accessor : item,
        }));

        const built = [
            {
                Header : this.props.header,
                columns,
            },
        ];        
        return built;
    }

        

    render() {


        return (
            <div>
                <ReactTable
                    data={this.props.data}
                    columns={this.columnsBuilder()} // Remove Props
                    defaultPageSize={10}
                    className="-striped -highlight"
                />
                <br />
            </div>
        );
    }
}

FIX ResponseTable columnsBuilder()

        if(this.props.data.length === 0) {
            return [{
                Header : this.props.header
            }];
        }
like image 268
zemmsoares Avatar asked May 23 '18 14:05

zemmsoares


2 Answers

If you are using the npm module react-table

import ReactTable from 'react-table';
import "react-table/react-table.css";

then just set attribute minRows to 0 to tag ReactTable like as below code to stop rendering empty rows

<ReactTable data={tableDataArray} minRows={0} />
like image 75
Manoj Rana Avatar answered Nov 19 '22 01:11

Manoj Rana


You can either do this in your parent container

{
  filteredCompletedIssues.length && <ResponseTable data={filteredCompletedIssues} />
}

OR you can do length check in your ResponseTable Component

render({data = []}) {

    if (!data.length) {
      return null;
    }

    return (

So your example would become:

import React from 'react';
import ReactTable from 'react-table';
import "react-table/react-table.css";
 
export default class ResponseTable extends React.Component {

    constructor(props) { // Use Props
        super(props); // Use Props
        this.columnsBuilder = this.columnsBuilder.bind(this);

    }
    columnsBuilder () { //Remove data
        if(this.props.data == 0){
           return
        }
        const props = Object.keys(this.props.data[0]); //Use Props
        const columns = props.map( (item, index) => ({
            Header : item,
            accessor : item,
        }));

        const built = [
            {
                Header : this.props.header,
                columns,
            },
        ];        
        return built;
    }

        

    render() {
        let { data = [] } = this.props;
        
        if(!data.length)
          return null;

        return (
            <div>
                <ReactTable
                    data={data}
                    columns={this.columnsBuilder()} // Remove Props
                    defaultPageSize={10}
                    className="-striped -highlight"
                />
                <br />
            </div>
        );
    }
}
like image 27
Rikin Avatar answered Nov 19 '22 01:11

Rikin