React JS: 此页面正在减慢Firefox速度,为加快浏览器,请停止此页面。

huangapple go评论66阅读模式
英文:

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 &#39;react&#39;
import { Link } from &quot;react-router-dom&quot;;

export default class Navbar extends Component {

  render() {
    return (
      &lt;div style={{display:&#39;flex&#39;, padding:&#39;0.5&#39;}}&gt;

        &lt;Link to=&quot;/&quot; style={{textDecoration:&#39;none&#39;}}&gt;
        &lt;h1 style={{marginTop:&#39;1rem&#39;,marginLeft:&#39;1rem&#39;}}&gt;Movies App&lt;/h1&gt;
        &lt;/Link&gt;

        &lt;Link to=&#39;/favourites&#39; style={{textDecoration:&#39;none&#39;}}&gt;
        &lt;h2 style={{marginLeft :&#39;5rem&#39;,marginTop:&#39;2rem&#39;}}&gt;Favorites&lt;/h2&gt;
        &lt;/Link&gt;
      
     
      &lt;/div&gt;
    )
  }
}

Here is my APP.js

import logo from &#39;./logo.svg&#39;;
import &#39;./App.css&#39;;
import React, { Component }  from &#39;react&#39;;
import Navbar from &#39;./Components/Navbar&#39;;
import Banner from &#39;./Components/Banner&#39;;
import Movies from &#39;./Components/Movies&#39;;
import Favourite from &#39;./Components/Favourite&#39;;
import {Switch,Route, BrowserRouter,Routes} from &#39;react-router-dom&#39;;

function App() {
  return (
    // &lt;&gt;
    // &lt;Navbar&gt;&lt;/Navbar&gt;
    // &lt;Banner&gt;&lt;/Banner&gt;
    // &lt;Movies&gt;&lt;/Movies&gt;
    // &lt;/&gt;
    &lt;BrowserRouter&gt;
      &lt;Navbar/&gt;
      &lt;Routes&gt;
      
      &lt;Route exact path=&quot;/&quot; element={&lt;HomePage/&gt;}/&gt;
      &lt;Route exact path=&quot;/favourites&quot; element={&lt;Favourite/&gt;}/&gt;
      
      &lt;/Routes&gt;
    
    &lt;/BrowserRouter&gt;
  );
}

function HomePage() {
  return (
    &lt;&gt;
      &lt;Banner /&gt;
      &lt;Movies /&gt;
    &lt;/&gt;
  );
}

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.

huangapple
  • 本文由 发表于 2023年2月7日 01:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定