Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2300
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T21:11:49+00:00 2022-05-31T21:11:49+00:00

reactjs – Cause of infinite re-render loop in a class component?

  • 0

[ad_1]

I’m relatively new to React and trying to understand how this code I’m working with is causing an infinite render loop. I’ve written an example of the code below that shows the issue. The error message I’m getting is: “Too many re-renders. React limits the number of renders to prevent an infinite loop.”

The props being passed to DataTable appear to be causing the issue. When one of the lines is commented out, there isn’t any problems. However, when both data and paginationDefaultPage are passed in, there’s an infinite loop. data is being passed as a prop to CustomDataTable and in this example the data is just a hardcoded array.

My current understanding is that a re-render should only occur when state is changed, but as far as I can tell, state isn’t being changed (the state is initialized in the CustomDataTable constructor but then never changed).

I’ve posted the code below, but you can also view it here: https://codesandbox.io/s/bitter-fire-g948qi

App.js

import "./styles.css";
import CustomDataTable from "./CustomDataTable";

// Utility function
function convertUnixTimestamp(timestamp) {
  const d = new Date(timestamp);

  const month = d.getMonth().toString().padStart(2, "0");
  const date = d.getDate().toString().padStart(2, "0");

  const hour = d.getUTCHours().toString().padStart(2, "0");
  const minutes = d.getMinutes().toString().padStart(2, "0");
  const seconds = d.getSeconds().toString().padStart(2, "0");

  return `${d.getFullYear()}/${month}/${date} ${hour}:${minutes}:${seconds}`;
}

export default function App() {
  const columns = [
    { name: "Import ID", selector: (row) => row[0], sortable: true },
    {
      name: "Timestamp",
      selector: (row) => convertUnixTimestamp(row[1]),
      sortable: true
    }
  ];

  return (
    <div className="App">
      <CustomDataTable
        columns={columns}
        data={[["239482398492839482", "2022-05-31 16:03"]]}
      />
    </div>
  );
}

CustomDataTable.js

import React from "react";
import DataTable from "react-data-table-component-with-filter";

class CustomDataTable extends React.Component {
  constructor(props) {
    super(props);

    console.log("CustomDataTable constructor");

    this.state = {
      data: props.data,
      filteredItems: props.data,
      filterText: "",
      resetPaginationToggle: false
    };
  }

  render() {
    // Uncommenting data={this.state.filteredItems} and commenting out paginationDefaultPage={this.state.resetPaginationToggle} 
    // prevents the infinite loop and shows the data as expected with no problems

    // Commenting out data={this.state.filteredItems} and leaving paginationDefaultPage= 
    // {this.state.resetPaginationToggle} doesn't cause any problems

    // Uncommenting both lines causes an infinite loop

    return (
      <DataTable
        columns={this.props.columns}
        // data={this.state.filteredItems}
        paginationDefaultPage={this.state.resetPaginationToggle}
        pagination
        persistTableHead
        subHeader
      />
    );
  }
}

export default CustomDataTable;

[ad_2]

  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4995 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1084 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1028 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.