未捕获错误:不变违规:元素类型无效:预期为字符串(用于内置组件)或类/函数但得到:对象
2015-12-07
1104983
我收到此错误:
Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
这是我的代码:
var React = require('react')
var ReactDOM = require('react-dom')
var Router = require('react-router')
var Route = Router.Route
var Link = Router.Link
var App = React.createClass({
render() {
return (
<div>
<h1>App</h1>
<ul>
<li><Link to="/about">About</Link></li>
</ul>
</div>
)
}
})
var About = require('./components/Home')
ReactDOM.render((
<Router>
<Route path="/" component={App}>
<Route path="about" component={About} />
</Route>
</Router>
), document.body)
我的
Home.jsx
文件:
var React = require('react');
var RaisedButton = require('material-ui/lib/raised-button');
var Home = React.createClass({
render:function() {
return (
<RaisedButton label="Default" />
);
},
});
module.exports = Home;
3个回答
就我的情况( 使用 Webpack )而言,两者的区别在于:
import {MyComponent} from '../components/xyz.js';
与
import MyComponent from '../components/xyz.js';
第二个可以正常工作,而第一个会导致错误。或者相反。
JohnL
2016-03-28
您需要 export default 或者 require(path).default
var About = require('./components/Home').default
jackypan1989
2015-12-07
您是否刚刚模块化了任何 React 组件?如果是,如果您忘记指定 module.exports ,您将收到此错误,例如:
未模块化的先前有效的组件/代码:
var YourReactComponent = React.createClass({
render: function() { ...
使用 module.exports 模块化的组件/代码:
module.exports = React.createClass({
render: function() { ...
charlez
2016-01-28