gpt4 book ai didi

reactjs - React-google-maps 信息窗口 `React.Children.only expected to receive a single React element child.`

转载 作者:行者123 更新时间:2023-12-03 22:57:54 26 4
gpt4 key购买 nike

我正在尝试在单击时打开特定标记上的信息窗口,但是当我单击一个时,它们似乎都在打开,并向我显示此错误:React.Children.only expected to receive a single React element child.
这就是我的代码现在的样子:

import React, { Component } from 'react';
import { GoogleMap, Marker, withGoogleMap, withScriptjs, InfoWindow } from 'react-google-maps'

class Map extends Component {
state = {
isOpen: false
}

handleMarkerClick = () => {
this.setState({ isOpen: true})
}

render() {
return(
<div>
<GoogleMap
defaultZoom={13}
defaultCenter={{ lat: -22.9034778, lng: -43.1264636 }}
>{this.props.markers.map((marker, index) =>(
<Marker
key={index}
position={marker.location}
onClick={this.handleMarkerClick}
>{this.state.isOpen && <InfoWindow onCloseClick={this.handleMarkerClick}/>}
</Marker>
))}
</GoogleMap>
</div>
)
}
}

export default withScriptjs(withGoogleMap(Map))

开始编辑

我做了一些更改以尝试解决评论,但是它还没有工作,你能给我一些关于我做错了什么的提示吗,因为我对顶部组件做了一些更改,我也会将它粘贴在这里:
import React, { Component } from 'react';
import Map from './Map.js'
import List from './List.js'
import escapeRegExp from 'escape-string-regexp'
import sortBy from 'sort-by'

class App extends Component {
state ={
locations:[
{
name: "Paróquia Nossa Senhora das Dores do Ingá",
location: {
lat: -22.9038875,
lng: -43.1252873
},
isOpen:false,
},
{
name: "Museu de Arte Contemporanea de Niteroi",
location: {
lat: -22.9078182,
lng: -43.1262919
},
isOpen:false,
},
{
name: "UFF - Faculdade de Direito",
location: {
lat: -22.9038469,
lng: -43.126024
},
isOpen:false,
},
{
name: "Ponte Rio-Niterói",
location: {
lat: -22.8701,
lng: -43.167
},
isOpen:false,
},
{
name: "Fundação Oscar Niemeyer",
location: {
lat: -22.888533927137285,
lng: -43.12815992250511
},
isOpen:false,
},
{
name: "Campo de São Bento",
location: {
lat: -22.905279,
lng: -43.107759
},
isOpen:false,
}
],
query:''
}

onToggleOpen = (location) => {
this.setState({ isOpen: !this.isOpen })
}

updateQuery = (query) => {
this.setState({ query: query.trim() })
console.log(query)
}

componentDidMount() {}
render() {
const { query, locations } = this.state

let filteredMarkers
if(query) {
const match = new RegExp(escapeRegExp(query), 'i')
filteredMarkers = locations.filter((location) => match.test(location.name))
}else {
filteredMarkers = locations
}

filteredMarkers.sort(sortBy('name'))

return (
<div className="App">
<div style={{height:`5vh`}}>
<input
type='text'
placeholder='Search locations'
value={query}
onChange={(event) => this.updateQuery(event.target.value)}
/>
</div>
<List
markers={filteredMarkers}
/>
<Map
onToggle={this.onToggleOpen}
googleMapURL="https://maps.googleapis.com/maps/api/js?&key=AIzaSyAiqO5W1p5FAFf8RZD11PGigUXSlmVHguQ&v=3"
loadingElement={<div style={{ height: `100%` }} />}
containerElement={<div style={{ height: `80vh` }} />}
mapElement={<div style={{ height: `100%` }} />}
className="Map"
markers={filteredMarkers}
/>
</div>
);
}
}

export default App;

map .js
import React, { Component } from 'react';
import { GoogleMap, Marker, withGoogleMap, withScriptjs, InfoWindow } from 'react-google-maps'

class Map extends Component {
render() {
return(
<div>
<GoogleMap
defaultZoom={13}
defaultCenter={{ lat: -22.9034778, lng: -43.1264636 }}
>{this.props.markers.map((marker, index) =>(
<Marker
key={index}
position={marker.location}
onClick={() => this.props.onToggle(marker)}
>{marker.isOpen && <InfoWindow onCloseClick={this.ontoggleOpen}>Hello</InfoWindow>}
</Marker>
))}
</GoogleMap>
</div>
)
}
}

export default withScriptjs(withGoogleMap(Map))

最佳答案

我遇到的问题 React.Children.only expected to receive a single React element child.是因为我没有在信息窗口中设置一个 div 导致的,所以只需添加它,这个特定问题就解决了。

这是它过去的样子:
<InfoWindow onCloseClick={this.handleMarkerClick}/>
这是它的样子:<InfoWindow onCloseClick={()=>this.props.onToggle(marker)}><div>Hello</div></InfoWindow>
或类似的东西。

关于reactjs - React-google-maps 信息窗口 `React.Children.only expected to receive a single React element child.`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47731572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com