javascript - 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)React JS
问题描述
我正在尝试学习反应,但我不理解这个错误。如果我返回一个字符串,那很好,但如果我返回,它会一直给我这个错误。
| | | | 错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。
MapsContainer.js
import {GoogleMapReact, withGoogleMap, GoogleMap, Map} from 'google-map-react';
import React, {Component} from 'react';
class MapsContainer extends Component {
static defaultProps = {
center: {lat: 23.2341, lng:5.12342},
zoom: 8
}
render() {
return(
<div>
<GoogleMapReact
bootstrapURLKeys={{key: 'kjk'}}
defaultCenter={this.props.center}
defaultZoom={this.props.zoom}
> </GoogleMapReact>
{/* <GoogleMapReact
bootstrapURLKeys={{
key: 'YOUR_API_KEY',
libraries: ['places', 'directions']
}}
defaultZoom={11}
defaultCenter={{ lat: 12.2341, lng: 23.31234 }}
yesIWantToUseGoogleMapApiInternals={true}
onGoogleApiLoaded={({ map, maps }) => this.apiHasLoaded(map, maps)} // "maps" is the mapApi. Bad naming but that's their library.
>
</GoogleMapReact> */}
</div>
);
}
};
export default MapsContainer;
... 应用程序.js
import React from 'react';
import logo from './beer.svg';
import './App.css';
import MapsContainer from './containers/MapsContainer';
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
</header>
<MapsContainer/>
</div>
);
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(
<App />,
document.getElementById('root')
);
解决方案
我终于明白我做不到
从 'google-map-react' 导入 {GoogleMapReact, withGoogleMap, GoogleMap, Map};
相反,它必须是
从“google-map-react”导入 GoogleMapReact;
推荐阅读
- java - Android SDK 项目导入 Eclipse
- nlp - 如何使用 gensim word2vec 处理大量句子?
- elasticsearch - EFK Kubernetes 堆栈中不存在主机日志日志
- ruby - Ruby - IPSocket.getaddress 返回 ipv6 地址
- python - 有没有办法在没有给定分隔符的情况下通过其内容的某些元素对数据框进行分组?
- php - 登录尝试显示空白页面后未显示任何错误
- python - 在 Sphinx 中指向 reStructuredText 锚点的 Markdown 链接
- mongodb - MongoDB:如何在彼此之间的设定时间内查询文档
- c++ - 当用户单击禁用的窗口(通过 EnableWindow(hwnd,FALSE)禁用)时,如何以编程方式阻止 Windows 播放“默认哔”声?
- php - 内部连接来自 MYSQL 工作台中不同表的 2 列