英文:
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:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- 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 (
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 'react';
import { Link } from 'react-router-dom';
const Header = () => {
return (
<div className="header flex justify-between items-center py-4 px-6">
<div className="links flex justify-end w-2/3">
<Link to="/" className="mx-4 text-purple-600 font-cursive"> Home </Link>
<Link to="/services" className="mx-4 text-purple-600 font-cursive"> Services </Link>
<Link to="/contact" className="mx-4 text-purple-600 font-cursive"> Contact </Link> </div>
<div className="title"> <h1 className="text-3xl text-pink-600 font-cursive">The Salon</h1> </div> </div>);
};
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>
<Header />
</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.
答案1
得分: 1
以下是翻译好的内容:
看起来你的代码存在一些问题,可能导致你提到的错误。
首先,在你的 index.js 文件中,你应该导入 ReactDOM.render 而不是 ReactDOM.createRoot。createRoot 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 库。它应该作为依赖项使用 npm 或 yarn 安装,然后像这样导入:
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('root'));
With this:
const root = document.getElementById('root');
And change the root.render() method call to ReactDOM.render():
ReactDOM.render(
<React.StrictMode>
<Header />
</React.StrictMode>,
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 'react-router-dom';
Also, make sure that you have wrapped your Header component inside a BrowserRouter component in your index.js file, like this:
import { BrowserRouter } from 'react-router-dom';
ReactDOM.render(
<BrowserRouter>
<React.StrictMode>
<Header />
</React.StrictMode>
</BrowserRouter>,
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论