我对这个错误消息可能意味着什么或为什么我只在尝试使用react-router时得到它感到困惑。如果我直接渲染组件,该应用程序工作正常:render(<App />, document.getElementById('root'));
但是当我尝试使用<BrowserRouter>
,<Match>',
&amp;来自<Miss>
的{{1}}元素,如下所示: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文件中看到此错误:.
这是我的index.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...
有没有人知道这里有什么问题?
我不确定这是否有用,但这里是webpack数据,其中包含一些关于错误可能位置的线索:
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'));
**可能导致问题的其他组件: 这是我的App.js文件:
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;
这是SineWave.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;
最后,NotFound.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;
答案 0 :(得分:2)
已解决:事实证明<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 -
答案 1 :(得分:0)
您是否尝试过像这样渲染Main?:
render(
(
<Main/>
),
document.getElementById("selector")
);
请注意,我已将主要组件包围在括号中。
这可能是一个愚蠢的解决方案,但我知道我之前遇到过这个问题,我认为这是渲染的问题。