当我试图渲染react-leaflet-search时,我得到了一个错误。当我尝试使用该错误的解决方案时,react正在编译,但组件似乎没有呈现。
我尝试了他们的示例simple search。得到相同的错误
TypeError: Cannot read property 'map' of undefined
new ReactLeafletSearch
C:/Users/project/node_modules/react-leaflet-search/lib/React-Leaflet-Search.js:106
103 | });
104 | _this.SearchInfo = null; // searched lat,lng or response from provider
105 |
> 106 | _this.map = context.map || props.leaflet.map;
| ^ 107 | return _this;
108 | }
109 | 我尝试了这个解决方案(从react-leaflet-search页面)
const searchComponent = props => (<ReactLeafletSearch position="topleft" />);我甚至试过这个:
const searchComponent = withLeaflet(ReactLeafletSearch);这些代码行绕过了"error“,但似乎没有组件正在呈现。
我的react-leaflet Map组件正在加载,还有缩放+/-按钮,但没有搜索。
这是我使用map example的代码,当然我也是这样做的:
import { ReactLeafletSearch } from "react-leaflet-search";唯一的区别是在第31行之后添加了我的组件。
31. </Marker>
32. <searchComponent></searchComponent>
33. </Map>而且看起来什么都没有渲染。
我期望得到的结果是一个搜索组件,如此 working example 中所示
发布于 2019-09-24 14:35:23
要重现this示例,需要执行以下操作:
安装leaflet、react-leaflet、react-leaflet-search。
之后,你需要安装babel-polyfill,如果你使用react-leaflet v2,你应该用withLeaflet方法包装这个组件。它也被记录为github issue。
const ReactLeafletSearchComponent = withLeaflet(ReactLeafletSearch);在示例的SimpleExample组件的render方法中:
export default class SimpleExample extends Component {
...
render() {
// here wrap it
const ReactLeafletSearchComponent = withLeaflet(ReactLeafletSearch);
return (
<Map
className="simpleMap"
scrollWheelZoom={true}
bounds={this.state.bounds}
maxZoom={this.state.maxZoom}
maxBounds={this.state.maxBounds}
>
<TileLayer
noWrap={true}
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<ReactLeafletSearchComponent
customProvider={this.provider}
position="topleft"
inputPlaceholder="Custom placeholder"
search={[33.100745405144245, 46.48315429687501]}
showMarker={true}
zoom={5}
showPopup={true}
popUp={this.customPopup}
closeResultsOnClick={true}
openSearchOnLoad={true}
// // these searchbounds would limit results to only Turkey.
searchBounds={[
[33.100745405144245, 46.48315429687501],
[44.55916341529184, 24.510498046875]
]}
// providerOptions={{region: 'tr'}}
// default provider OpenStreetMap
// provider="BingMap"
// providerKey="AhkdlcKxeOnNCJ1wRIPmrOXLxtEHDvuWUZhiT4GYfWgfxLthOYXs5lUMqWjQmc27"
/>
</Map>
);
}
}也不要忘记添加map height和导入leaflet.css
发布于 2019-09-24 17:10:50
kboul的回答帮助我解决了搜索组件不能正确呈现的问题。
const ReactLeafletSearchComponent = withLeaflet(ReactLeafletSearch);在此之后,您可以使用"ReactLeafletSearchComponent“,组件将呈现。
我对变量使用了不同的名称。它似乎在一定程度上是名称敏感的。(我不完全理解)但是当我尝试使用像"searchComp“这样的名字时,它不会工作,但是当我尝试使用"ReactLeafletSearchComponent”时,它工作得很好。
https://stackoverflow.com/questions/58071816
复制相似问题