gpt4 book ai didi

javascript - 使用 react-router 时出现 React 错误 "Uncaught Invariant Violation"

转载 作者:行者123 更新时间:2023-11-29 21:14:28 26 4
gpt4 key购买 nike

我对这个错误消息可能意味着什么或者为什么我只在尝试使用 react-router 时才得到它感到困惑。如果我像这样直接渲染一个组件,应用程序工作正常:render(<App />, document.getElementById('root'));

但是当我尝试使用 <BrowserRouter> , <Match>', & <Miss>来自 react-router 的元素,像这样:render(<Main />, document.getElementById('root')); ,我从控制台收到以下错误消息:

Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of主要.

我还在 invariant.js 文件中看到此错误:error.framesToPop = 1; // we don't care about invariant's own frame error = Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined...

这是我的 index.js 文件

import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter, Match, Miss } from 'react-router';
import App from './App';
import SineWave from './SineWave';
import NotFound from './NotFound';
import './index.css';

const Main = () => {
return (
<BrowserRouter>
<div>
<Match exactly pattern="/" component={App} />
<Match exactly pattern="/lesson-one" component={SineWave} />
<Miss component={NotFound} />
</div>
</BrowserRouter>
)
}

render(<Main />, document.getElementById('root'));

有人知道这里出了什么问题吗?

我不确定这是否有用,但这里是 webpack 数据,其中包含一些关于错误可能出在哪里的线索:

function invariant(condition, format, a, b, c, d, e, f) {
if (true) {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
}

if (!condition) {
var error;
if (format === undefined) {
error = new Error('Minified exception occurred; use the non-minified dev environment ' + 'for the full error message and additional helpful warnings.');
} else {
var args = [a, b, c, d, e, f];
var argIndex = 0;
error = new Error(format.replace(/%s/g, function () {
return args[argIndex++];
}));
error.name = 'Invariant Violation';
}

error.framesToPop = 1; // we don't care about invariant's own frame
throw error;
}
}

module.exports = invariant;

**可能导致问题的其他组件:这是我的 App.js 文件:

import React from 'react';
import './App.css';
import Header from './Header';
import Instructions from './Instructions';
import SineWave from './SineWave';


const App = (props) => {
return (
<div className="App">
<div className="header">
<Header />
</div>
<div className="body">
<div className="instructions">
<Instructions instructionText="Here are the instructions!"/>
</div>
<div className="SineWave">
<SineWave soundText="This is a sound."/>
</div>
</div>
</div>);
};

export default App;

这里是 SineWave.js 文件,也被引用:

import React from 'react';

class SineWave extends React.Component {
render() {
return (
<div>
<button classID="playSine">PLAY SINEWAVE</button>
<p>{this.props.soundText}</p>
</div>
)
}
}

export default SineWave;

最后是 NotFound.js 文件:

import React from 'react';

class NotFound extends React.Component {
render() {
return (
<h2>Not Found!111!!</h2>
)
}
}

export default NotFound;

最佳答案

已解决:事实证明 <BrowserRouter>只能在 react-router v4 中使用...我使用的是 v2.8,这是您键入 npm install --save react-router 时安装的版本.如果你想使用 react-router v4,你需要使用 npm install --save react-router@next .这是 GitHub 上 v4 分支的链接:https://github.com/ReactTraining/react-router/blob/v4/README.md

关于javascript - 使用 react-router 时出现 React 错误 "Uncaught Invariant Violation",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40018335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com