Developer menu shows errors in the app, but server launches successfully

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

Developer menu shows errors in the app, but server launches successfully

问题

I have some code for a React Application, and there's no errors when the app is tested, but it shows a blank page in the server. Upon closer inspection, it shows things like "Invalid hook call" or "uncaught type error" within the browser developer window. These are the exact errors

react.development.js:209 Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app

Here's the App.js files and the Index.js files. I'm using tailwindcss that's imported in the index.css file. I haven't yet made the pages for the links present in the header.

App.js

import React from 'react';
import { Link } from 'react-router-dom';
const Header = () => {
return (

Home
Services
Contact

The Salon

);
};
export default Header;

Index.js -

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import Header from './App';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>


</React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint.

英文:

I have some code for a React Application, and there's no errors when the app is tested, but it shows a blank page in the server. Upon closer inspection, it shows things like "Invalid hook call" or "uncaught type error" within the browser developer window. These are the exact errors

react.development.js:209  Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app

Here's the App.js files and the Index.js files. I'm using tailwindcss that's imported in the index.css file. I haven't yet made the pages for the links present in the header.

App.js

import React from &#39;react&#39;;
import { Link } from &#39;react-router-dom&#39;;
const Header = () =&gt; {
  return (
    &lt;div className=&quot;header flex justify-between items-center py-4 px-6&quot;&gt;
      &lt;div className=&quot;links flex justify-end w-2/3&quot;&gt;
        &lt;Link to=&quot;/&quot; className=&quot;mx-4 text-purple-600 font-cursive&quot;&gt; Home &lt;/Link&gt;
        &lt;Link to=&quot;/services&quot; className=&quot;mx-4 text-purple-600 font-cursive&quot;&gt; Services &lt;/Link&gt;
        &lt;Link to=&quot;/contact&quot; className=&quot;mx-4 text-purple-600 font-cursive&quot;&gt; Contact &lt;/Link&gt; &lt;/div&gt;
      &lt;div className=&quot;title&quot;&gt; &lt;h1 className=&quot;text-3xl text-pink-600 font-cursive&quot;&gt;The Salon&lt;/h1&gt; &lt;/div&gt; &lt;/div&gt;);
};
export default Header;

Index.js -

import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom/client&#39;;
import &#39;./index.css&#39;;
import Header from &#39;./App&#39;;
import reportWebVitals from &#39;./reportWebVitals&#39;;

const root = ReactDOM.createRoot(document.getElementById(&#39;root&#39;));
root.render(
  &lt;React.StrictMode&gt;
    &lt;Header /&gt;
  &lt;/React.StrictMode&gt;
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint.

答案1

得分: 1

以下是翻译好的内容:

看起来你的代码存在一些问题,可能导致你提到的错误。

首先,在你的 index.js 文件中,你应该导入 ReactDOM.render 而不是 ReactDOM.createRootcreateRoot API 用于并发模式,这是React中的一个实验性功能,不建议在生产环境中使用。

所以,将这行代码:

const root = ReactDOM.createRoot(document.getElementById('root'));

替换为这样:

const root = document.getElementById('root');

并将 root.render() 方法调用改为 ReactDOM.render()

ReactDOM.render(
  <React.StrictMode>
    <Header />
  </React.StrictMode>,
  root
);

其次,在你的 App.js 文件中,请确保你已经正确导入了 react-router-dom 库。它应该作为依赖项使用 npmyarn 安装,然后像这样导入:

import { Link } from 'react-router-dom';

还要确保你在你的 index.js 文件中将 Header 组件包装在 BrowserRouter 组件中,像这样:

import { BrowserRouter } from 'react-router-dom';
ReactDOM.render(
  <BrowserRouter>
    <React.StrictMode>
      <Header />
    </React.StrictMode>
  </BrowserRouter>,
  root
);

通过这些更改,你的代码应该能够在没有错误的情况下渲染 Header 组件,然后你可以继续创建头部链接的页面。

英文:

It looks like there are a few issues with your code that could be causing the errors you mentioned.

Firstly, in your index.js file, you should be importing ReactDOM.render instead of ReactDOM.createRoot. The createRoot API is used for concurrent mode, which is an experimental feature in React and not recommended for production use yet.

So, replace this line:

const root = ReactDOM.createRoot(document.getElementById(&#39;root&#39;));

With this:

const root = document.getElementById(&#39;root&#39;);

And change the root.render() method call to ReactDOM.render():

ReactDOM.render(
 &lt;React.StrictMode&gt;
  &lt;Header /&gt;
 &lt;/React.StrictMode&gt;,
 root
);

Secondly, in your App.js file, make sure that you have imported the react-router-dom library properly. It should be installed as a dependency using npm or yarn, and then imported like this:

import { Link } from &#39;react-router-dom&#39;;

Also, make sure that you have wrapped your Header component inside a BrowserRouter component in your index.js file, like this:

import { BrowserRouter } from &#39;react-router-dom&#39;;
ReactDOM.render(
 &lt;BrowserRouter&gt;
  &lt;React.StrictMode&gt;
   &lt;Header /&gt;
  &lt;/React.StrictMode&gt;
 &lt;/BrowserRouter&gt;,
 root
);

With these changes, your code should be able to render the Header component without any errors, and you can proceed to create the pages for the links in the header.

huangapple
  • 本文由 发表于 2023年4月17日 06:37:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030664.html
匿名

发表评论

匿名网友

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

确定