Niishaw
Niishaw

Reputation: 141

error too many re-renders. react limits the number of renders to prevent an infinite loop

It seems that calling the todoList which is mapped to state is causing the issue, but I don't know why...how do I get the array of todos in my state to display in the table without this error?

import React, { useState, useEffect } from 'react';
import { connect } from 'react-redux';
import { getTodos } from '../../actions/todo';

//Bootstrap Table
import BootstrapTable from 'react-bootstrap-table-next';
import paginationFactory from 'react-bootstrap-table2-paginator';
import * as ReactBootStrap from 'react-bootstrap';

const UserTable = ({ getTodos, todoList }) => {
  useEffect(() => {
    getTodos();
    // eslint-disable-next-line
  }, []);

  const [todos, setTodos] = useState([]);
  const [loading, setLoading] = useState(false);

  const columns = [
    { dataField: 'id', text: 'ID' },
    { dataField: 'userId', text: "User's ID" },
    { dataField: 'title', text: 'Title of Todo' },
    { dataField: 'completed', text: 'Is this done?' },
  ];

  setTodos(todoList);

  return (
    <BootstrapTable
      keyField='id'
      data={todos}
      columns={columns}
      pagination={paginationFactory()}
    />
  );
};

const mapStateToProps = (state) => ({
  todoList: state.todo.todoList,
});

export default connect(mapStateToProps, { getTodos })(UserTable);

Upvotes: 2

Views: 123

Answers (1)

kind user
kind user

Reputation: 41893

Consider avoiding setting the state directly inside function body - it will cause an endless loop.

Anyways - why do you want to keep it inside state? I would suggest you to operate on the props.

return (
  <BootstrapTable
    keyField='id'
    data={todoList} // todos directly from props
    columns={columns}
    pagination={paginationFactory()}
  />
);

Upvotes: 2

Related Questions