React giving me Error: Too many re-renders. React limits the number of renders to prevent an infinite loop

Emma Li

The handleSortByChange function gives me an error on the browser stating that “Too many re-renders. React limits the number of renders to prevent an infinite loop.” However, on the terminal, it indicates that compliled successfully

import React, {useState} from 'react';
import './SearchBar.css';

const sortByOptions = {
    'Best Match': 'best_match',
    'Highest Rated': 'rating',
    'Most Reviewed': 'review_count'
}

function SearchBar() {
    const [ sortBy, setSortBy ] = useState('best_match')

    const getSortByClass = (sortByOption) => {
        if(sortBy === sortByOption) {
            return 'active';
        } else {
            return '';
        }
    }

    const handleSortByChange = (sortByOption) => {
        setSortBy(sortByOption);
    }

    const renderSortByOptions = () => {
        return Object.keys(sortByOptions).map((sortByOption) => {
            let sortByOptionValue = sortByOptions[sortByOption];
            return <li onClick={handleSortByChange(sortByOptionValue)} className={getSortByClass(sortByOptionValue)} key={sortByOptionValue}>{sortByOption}</li>
        })
    }

    return(
        <div className="SearchBar">
            <div className="SearchBar-sort-options">
                <ul>
                    {renderSortByOptions()}
                </ul>
            </div>
            <div className="SearchBar-fields">
                <input placeholder="Search Businesses" />
                <input placeholder="Where?" />
            </div>
            <div className="SearchBar-submit">
                {/* <a href="#">Let's Go</a> */}
                {/* <button>Let's Go</button> */}
            </div>
        </div>
    )
}

export default SearchBar;
Ketan Ramteke

On your <li> tag you are directly calling the method like,

onClick={handleSortByChange(sortByOptionValue)}, which will set the state and rerender will be triggered, the same thing will happen again in this render cycle too, and thus the infinite loop.

instead do following:

<li
          onClick={()=>handleSortByChange(sortByOptionValue)}
          className={getSortByClass(sortByOptionValue)}
          key={sortByOptionValue}
        >

in this way, the handleSortByChange(sortByOptionValue) will only get executed when the <li> element is clicked.

import React, { useState } from "react";
// import './SearchBar.css';

const sortByOptions = {
  "Best Match": "best_match",
  "Highest Rated": "rating",
  "Most Reviewed": "review_count"
};

function SearchBar() {
  const [sortBy, setSortBy] = useState("best_match");

  const getSortByClass = sortByOption => {
    if (sortBy === sortByOption) {
      return "active";
    } else {
      return "";
    }
  };

  const handleSortByChange = sortByOption => {
    setSortBy(sortByOption);
  };

  const renderSortByOptions = () => {
    return Object.keys(sortByOptions).map(sortByOption => {
      let sortByOptionValue = sortByOptions[sortByOption];
      return (
        <li
          onClick={()=>handleSortByChange(sortByOptionValue)}
          className={getSortByClass(sortByOptionValue)}
          key={sortByOptionValue}
        >
          {sortByOption}
        </li>
      );
    });
  };

  return (
    <div className="SearchBar">
      <div className="SearchBar-sort-options">
        <ul>{renderSortByOptions()}</ul>
      </div>
      <div className="SearchBar-fields">
        <input placeholder="Search Businesses" />
        <input placeholder="Where?" />
      </div>
      <div className="SearchBar-submit">
       
      </div>
    </div>
  );
}

export default SearchBar;

Working example: Stackblitz

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error: Too many re-renders. React limits the number of renders to prevent an infinite loop React Native

react usestate Error: Too many re-renders. React limits the number of renders to prevent an infinite loop

React : Error: Too many re-renders. React limits the number of renders to prevent an infinite loop

received this error "Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."

Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop - ProtectedRoutes Component

Error: "Too many re-renders. React limits the number of renders to prevent an infinite loop"

Too many re-renders. React limits the number of renders to prevent an infinite loop. Next js error

"Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."

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

useState and if statement causing Error: Too many re-renders. React limits the number of renders to prevent an infinite loop

Too many re-renders. React limits the number of renders to prevent an infinite loop-error

Error: Too many re-renders. React limits the number of renders to prevent an infinite loop when using useState()

Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop

Error: Too many re-renders. React limits the number of renders to prevent an infinite loop [another variant]

Too many re-renders. React Limits the number of renders to prevent an infinite loop - React hooks

React Native issue - Too many re-renders. React limits the number of renders to prevent an infinite loop

React Js : Too many re-renders. React limits the number of renders to prevent an infinite loop

Too many re-renders. React limits the number of renders to prevent an infinite loop. - React Hooks

React redux Too many re-renders.React limits the number of renders to prevent an infinite loop

Too many re-renders. React limits the number of renders to prevent an infinite loop | React Native

React Native: Too many re-renders. React limits the number of renders to prevent an infinite loop

Too many re-renders. React limits the number of renders to prevent an infinite loop. with for loop

ReactJS, react-bootstrap, Modal Box: "Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."

React: setting a state Hook causes error - Too many re-renders. React limits the number of renders to prevent an infinite loop

Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. (React-js)

Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop

Too many re-renders. React limits the number of renders to prevent an infinite loop

Too many re-renders. React limits the number of renders to prevent an infinite loop. useState problem?

Too many re-renders. React limits the number of renders to prevent an infinite loop - why?