reactjs - 从父组件的状态传递子组件中的道具
问题描述
我无法在 Google 地图组件上使用从父组件到子组件的状态。这听起来可能是一个非常基本的问题,但是我试图了解其中一个道具有效与其他无效之间的区别。
所以我有一个谷歌地图组件(从'google-map-react'导入GoogleMapReact)
static defaultProps = {
center: { lat: 62.10281, lng: -84.14565 },
zoom: 11
};
constructor(props) {
console.log(props);
super(props);
}
render() {
return (
<div className='google-map' style={{ height: '100%', width: '100%' }}>
<GoogleMapReact
bootstrapURLKeys={{ key: 'somekey' }}
center={this.props.center}
defaultZoom={ this.props.zoom }>
<AnyReactComponent
lat={ this.props.lat }
lng={ this.props.lng }
text={ 'Wheres Waldo?' }
/>
</GoogleMapReact>
</div>
)
}
然后从父组件
<MapComponent lat={this.state.latitude} lng={this.state.longitude}
center={{lat: this.state.latitude, lng: this.state.longitude}}
我已经在构造函数中检查了 lat、lng 和 center 的值,并且按预期得到它们仍然 map 不起作用
但是,如果我硬编码中心的值,让 lat 和 lng 从状态中获取值,那么它确实有效。
现在这有效,有人可以解释这个概念和区别以及如何克服它吗?感谢您的帮助。
<MapComponent lat={this.state.latitude} lng={this.state.longitude}
center={{lat: 62.10281, lng: -84.14565}}
解决方案
推荐阅读
- c - 如何使用 fscanf() 格式字符串
- hibernate - 我的 SpringEclipselink 项目运行良好。我想将 ORM 框架更改为 Hibernate。但它产生了上述问题
- java - Spring Boot中的外键引用主键
- c - 为什么我的编译器(VS2017)选择“CALL-JMP”来访问子程序而不是“CALL”?
- amazon-web-services - 查询 SQL Workbench 访问 Redshift 集群端点 IP 白名单
- android - 引起:java.lang.NoSuchMethodException:
[类 android.app.Application] - viewmodel - c# - 使用任务时是否必须锁定?
- docker - 如何通过 docker swarm 连接两台本地机器?
- reactjs - 如何在react.js中使用确认模式实现滑动删除
- javascript - 如何根据角度4中的日期过滤记录