英文:
React JS : this page is slowing down firefox, to speed up the browser , stop this page
问题
以下是翻译好的部分:
所以我有一个使用React制作的电影网站,它从TMBD获取数据,在主页上显示所有热门电影数据,而在"收藏"页面上显示所有收藏的电影。我使用路由连接了这两个页面,但当我尝试访问"收藏"页面时,我的浏览器变得非常慢,并告诉我停止网页,只有重新加载页面后,我才能被重定向到"收藏"页面,有解决方法吗?
这是我的导航栏,其中包含链接,当我尝试访问"收藏"页面时,它显示页面正在变慢。
import React, { Component } from 'react';
import { Link } from "react-router-dom";
export default class Navbar extends Component {
render() {
return (
<div style={{ display: 'flex', padding: '0.5rem' }}>
<Link to="/" style={{ textDecoration: 'none' }}>
<h1 style={{ marginTop: '1rem', marginLeft: '1rem' }}>Movies App</h1>
</Link>
<Link to='/favourites' style={{ textDecoration: 'none' }}>
<h2 style={{ marginLeft: '5rem', marginTop: '2rem' }}>Favorites</h2>
</Link>
</div>
)
}
}
这是我的APP.js文件。
import logo from './logo.svg';
import './App.css';
import React, { Component } from 'react';
import Navbar from './Components/Navbar';
import Banner from './Components/Banner';
import Movies from './Components/Movies';
import Favourite from './Components/Favourite';
import { Switch, Route, BrowserRouter, Routes } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Navbar/>
<Routes>
<Route exact path="/" element={<HomePage/>}/>
<Route exact path="/favourites" element={<Favourite/>}/>
</Routes>
</BrowserRouter>
);
}
function HomePage() {
return (
<>
<Banner />
<Movies />
</>
);
}
export default App;
当我尝试不使用路由访问各个文件时,一切正常,但当我尝试使用路由访问时,浏览器开始变慢,直到重新加载页面后数据才被获取。
我的GitHub仓库链接: https://github.com/faizxmohammad/React-MovieApp/tree/master/src
我希望能够在不减慢浏览器的情况下获取"收藏"页面的数据。
英文:
so I have a react movie website which fetches data from tmbd , on home page it shows all trending movies data , and on the favorites page it shows all favorite movies , i routed these two pages but when i try to access favorites my browser gets really slow and tells me to stop the webpage , only after reloading the page , i get redirected to favorites page any solution in this ?
this is my nav bar where the links are , when i try to access the favourites page its shows that the page is slowing down
import React, { Component } from 'react'
import { Link } from "react-router-dom";
export default class Navbar extends Component {
render() {
return (
<div style={{display:'flex', padding:'0.5'}}>
<Link to="/" style={{textDecoration:'none'}}>
<h1 style={{marginTop:'1rem',marginLeft:'1rem'}}>Movies App</h1>
</Link>
<Link to='/favourites' style={{textDecoration:'none'}}>
<h2 style={{marginLeft :'5rem',marginTop:'2rem'}}>Favorites</h2>
</Link>
</div>
)
}
}
Here is my APP.js
import logo from './logo.svg';
import './App.css';
import React, { Component } from 'react';
import Navbar from './Components/Navbar';
import Banner from './Components/Banner';
import Movies from './Components/Movies';
import Favourite from './Components/Favourite';
import {Switch,Route, BrowserRouter,Routes} from 'react-router-dom';
function App() {
return (
// <>
// <Navbar></Navbar>
// <Banner></Banner>
// <Movies></Movies>
// </>
<BrowserRouter>
<Navbar/>
<Routes>
<Route exact path="/" element={<HomePage/>}/>
<Route exact path="/favourites" element={<Favourite/>}/>
</Routes>
</BrowserRouter>
);
}
function HomePage() {
return (
<>
<Banner />
<Movies />
</>
);
}
export default App;
when i try to access the individual file without routing it works fine ,but when i try to access this with routing my browser starts to slow down and the data isnt fetched untill i reload the page
my Github repo link: https://github.com/faizxmohammad/React-MovieApp/tree/master/src
I am expecting to fetch data of favorites pages without slowing down the browser
答案1
得分: 0
我克隆了你的代码。
问题出在文件Favorites.js的第45行。你正在在渲染方法内部更改状态。这会导致循环。将任何状态更改移到渲染方法之外。为什么?这是工作流程:
状态更改 -> 渲染组件
你正在在渲染组件内部更改状态,所以它变成了:
状态更改 -> 渲染(内部更改状态,因此导致状态更改) -> 状态更改 -> 渲染(内部更改状态,因此导致状态更改)... 无限循环。
英文:
I cloned your code.
The problem is in the file Favorites.js, line 45. You are changing the state, inside the render method. This causes loops. Move any state change, outside render method. Why? This is the workflow:
State change -> renders the component
You are changing your state, inside render component, so it becomes:
state change -> render (state change inside so leads to state change) -> state change -> render render (state change inside so leads to state change) ... infinite loop.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论