![](/img/trans.png)
[英]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
如何防止出現以下錯誤:
太多的重新渲染。 React 限制了渲染的數量以防止無限循環。
我剛剛將基於 class 的組件更改為功能組件,但它不起作用
我的源代碼
import React, {Fragment, useState} from 'react';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import Navbar from './Components/Layout/Navbar';
import Users from './Components/users/Users';
import User from './Components/users/User';
import Search from './Components/users/Search';
import Alert from './Components/Layout/Alert';
import About from './Components/pages/About';
import './App.css';
import Axios from 'axios';
const App = () => {
const [users, setUsers] = useState( [] );
const [user, setUser] = useState( {} );
const [repos, setRepos] = useState( [] );
const [loading, setLoading] = useState( false );
const [alert, setAlert] = useState( null );
// Search Github Users
const searchUsers = async text => {
setLoading(true);
const res = await Axios.get(`https://api.github.com/search/users?q=${text}&client_id=${process.env.REACT_APP_GITHUB_CLIENT_ID}&client_secret=${process.env.REACT_APP_GITHUB_CLIENT_SECRET}`);
setUsers(res.data.items);
setLoading(false);
};
// GEt single github user
const getUser = async username => {
setLoading(true);
const res = await Axios.get(`https://api.github.com/users/${username}?&client_id=${process.env.REACT_APP_GITHUB_CLIENT_ID}&client_secret=${process.env.REACT_APP_GITHUB_CLIENT_SECRET}`);
setUser(res.data);
setLoading(false);
};
// Get users repos
const getUserRepos = async username => {
setLoading(true);
const res = await Axios.get(`https://api.github.com/users/${username}/repos?per_page=5&sort=created:asc&client_id=${process.env.REACT_APP_GITHUB_CLIENT_ID}&client_secret=${process.env.REACT_APP_GITHUB_CLIENT_SECRET}`);
setRepos(res.data);
setLoading(false);
};
// Clear users from state
const clearUsers = () =>
setUsers([]);
setLoading(false);
// Set ALert
const showAlert = (msg, type) => {
setAlert({msg, type});
setTimeout(()=> setAlert(null),3000);
};
return (
<Router>
<div className="App">
<Navbar />
<div className="container">
<Alert alert={alert} />
<Switch>
<Route exact path='/' render={props => (
<Fragment>
<Search
searchUsers={searchUsers}
clearUsers={clearUsers}
showClear={ users.length>0? true : false }
setAlert={showAlert}
/>
<Users loading={loading} users={users} />
</Fragment>
)} />
<Route exact path = '/about' component={About} />
<Route exact path= '/user/:login' render={props => (
<User
{...props}
getUser={getUser}
getUserRepos={getUserRepos}
user={user}
repos={repos}
loading={loading} />
)} />
</Switch>
</div>
</div>
</Router>
);
}
export default App;
我只是將基於 class 的組件更改為功能組件,我得到了這些錯誤。
0
如何防止出現以下錯誤:
太多的重新渲染。 React 限制了渲染的數量以防止無限循環。
從評論來看,
function clearUsers
的聲明有錯誤
const clearUsers = () => setUsers([]);
setLoading(false);
應該是。
const clearUsers = () => {
setUsers([]);
setLoading(false);
}
因為這個小錯字。 setLoading
function 在第一個渲染上被調用,然后調用 setLoading 觸發響應再次調用渲染並返回調用setLoading
並導致無限渲染。
我遇到了同樣的錯誤。 我發現我的問題是在function()的末尾有括號。 然而 function 應該在沒有括號的情況下被調用。 菜鳥錯誤。
前:
onClick={myFunction()}
后:
onClick={myFunction}
如果您使用的是按鈕,還請檢查“ onclick ”的大小寫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.