我得到这个错误:
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)
我的家。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;
除了进口/出口问题提到。我发现使用React.cloneElement()向子元素添加道具,然后渲染它给了我同样的错误。
我不得不改变:
render() {
const ChildWithProps = React.cloneElement(
this.props.children,
{ className: `${PREFIX}-anchor` }
);
return (
<div>
<ChildWithProps />
...
</div>
);
}
to:
render() {
...
return (
<div>
<ChildWithProps.type {...ChildWithProps.props} />
</div>
);
}
有关更多信息,请参阅React.cloneElement()文档。
@Balasubramani M救了我。想再加一个来帮助人们。当你把太多的东西粘在一起,对版本漫不经心时,这就是问题所在。我更新了material-ui的一个版本,需要更改
import Card, {CardContent, CardMedia, CardActions } from "@material-ui/core/Card";
:
import Card from '@material-ui/core/Card';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import CardMedia from '@material-ui/core/CardMedia';
我不相信我的情况和补救措施在这里讨论,所以我补充。
我有一个函数,它返回一个名为notification的对象
import IconProgress from '../assets/svg/IconProgress';
const getNotificationFromAttachment = (attachment) => {
const notification = {
Icon: IconProcessing,
iconProps: {},
};
...
notification.Icon = {IconProgress};
return notification
}
React期望一个字符串或类/函数作为变量通知的值。图标
所以你所需要做的就是将{IconProgress}改为IconProgress
见下图: