[英]React error "Uncaught Invariant Violation" when using react-router
I'm stumped on what this error message could mean or why I only get it when trying to use react-router.我很困惑这个错误消息可能意味着什么,或者为什么我只在尝试使用 react-router 时才得到它。 The app works fine if I render a component directly like this: render(<App />, document.getElementById('root'));
如果我像这样直接渲染组件,应用程序工作正常: render(<App />, document.getElementById('root'));
But when I try to use the <BrowserRouter>
, <Match>',
& <Miss>
elements from react-router
, like this: render(<Main />, document.getElementById('root'));
但是当我尝试使用react-router
的<BrowserRouter>
、 <Match>',
和<Miss>
元素时,就像这样: render(<Main />, document.getElementById('root'));
, I get the following error message from my console: ,我从控制台收到以下错误消息:
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
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
Main .
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
Main 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
.
I'm also seeing this error in an invariant.js file: 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...
我也在 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...
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...
Here's my index.js file这是我的 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'));
Does anyone have any idea what's wrong here?有谁知道这里有什么问题?
I'm not sure if this is useful, but here's the webpack data with some clues as to where the error might be:我不确定这是否有用,但这里是 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;
**Additional components that may be causing the problem: Here's my App.js file: **可能导致问题的其他组件:这是我的 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;
And here is the SineWave.js file, which is also referenced:这是 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;
And finally, the NotFound.js file:最后,NotFound.js 文件:
import React from 'react';
class NotFound extends React.Component {
render() {
return (
<h2>Not Found!111!!</h2>
)
}
}
export default NotFound;
SOLVED : It turns out that <BrowserRouter>
can only be used in react-router v4 ... I was using v2.8, which is the version that's is installed when you type npm install --save react-router
.已解决:事实证明<BrowserRouter>
只能在 react-router v4 中使用...我使用的是 v2.8,这是您键入npm install --save react-router
时安装的版本。 If you want to use react-router v4 you'll need to use npm install --save react-router@next
.如果你想使用 react-router v4,你需要使用npm install --save react-router@next
。 Here's a link to the v4 branch on GitHub:https://github.com/ReactTraining/react-router/blob/v4/README.md –这是 GitHub 上 v4 分支的链接:https ://github.com/ReactTraining/react-router/blob/v4/README.md –
Have you tried rendering Main like this?:你试过像这样渲染 Main 吗?:
render(
(
<Main/>
),
document.getElementById("selector")
);
Note that I have surrounded the Main component in brackets.请注意,我已将 Main 组件括在括号中。
It might be a dumb solution, but I know I've encountered this before and I believe it's an issue with rendering.这可能是一个愚蠢的解决方案,但我知道我以前遇到过这个问题,我相信这是渲染的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.