我得到这个错误:
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-native而不是native-base导入容器和内容。
错误,因为这一行:
import { ActivityIndicator, RefreshControl, StyleSheet, View, Keyboard, Container, Content } from 'react-native';
下面是代码修复:
import { ActivityIndicator, RefreshControl, StyleSheet, View, Keyboard } from 'react-native';
import { Container, Content} from 'native-base';
除了进口/出口问题提到。我发现使用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()文档。
对我来说,它是app config json的名称。在index.js中,导入是这样的:
import { AppRegistry } from 'react-native'
import App from './app'
import { name as appName } from './app.json'
AppRegistry.registerComponent(appName, () => App)
React Native不区分'。./app '和'./app.json'。不知道为什么。因此,我已经将app.json的名称更改为app.config.json,这已经解决了问题:
import { AppRegistry } from 'react-native'
import App from './app'
import { name as appName } from './app.config.json'
AppRegistry.registerComponent(appName, () => App)