我有一个我制作的地图应用程序,需要一些地图图标在按下按钮后显示/消失,但我无法弄清楚如何设置它以重新渲染组件,当我通过一个新的体育来自它的父母的财产:
父加载组件:
<SimpleMap sports=[default value and future values go here] />
简单地图组件(简化):
constructor(props) {
(props);
this.state = {
events: [{venue: {lat: 2, lon: 1}}],
sports: ["baseball", "football", "paddle", "soccer", "boxing", "dart", "biking", "golf", "hockey", "inline-skating", "tennis", "volleyball", "skateboard", "kickball", "bowling", "pool", "ride", "hike", "ice-skating"]
};
};
componentWillReceiveProps (nextProps) {
this.setState({events: [{venue: {lat: 2, lon: 1}}],
sports: nextProps.sports});
console.log(nextProps.sports);
}
static defaultProps = {
center: {lat: 36.160338, lng: -86.778780},
zoom: 12,
sports: ["baseball", "football", "paddle", "soccer", "boxing", "dart", "biking", "golf", "hockey", "inline-skating", "tennis", "volleyball", "skateboard", "kickball", "bowling", "pool", "ride", "hike", "ice-skating"],
};
makeMapEvents (insertProps) {
fetch("./json/meetup.json").then((response) => {
return response.json()
}).then((response) => {
/* eventually returns new events object based on insertProps */
this.setState({events: response});
}
};
componentDidMount () {
this.makeMapEvents(this.state.sports);
console.log("mounted", this.state.sports);
}
最终会在此处映射来自州的事件:
<GoogleMap>
{this.state.events.map((result) => {
return (<Marker key={counter++} lat={result.venue.lat} lng={result.venue.lon}
sport={this.props.sport} data={result}/>);
})}
</GoogleMap>
答案 0 :(得分:6)
默认情况下,React ES6类不会自动绑定this
到非反应基本成员方法。因此,函数this
中makeMapEvents
的上下文未正确绑定。有两种方法可以解决这个问题:
通过ES7属性初始化程序:
makeMapEvents = (insertProps) => {
fetch("./json/meetup.json").then((response) => {
return response.json()
}).then((response) => {
/* eventually returns new events object based on insertProps */
this.setState({events: response});
})
};
通过构造函数中的绑定:
constructor(props) {
(props);
this.state = {
events: [{venue: {lat: 2, lon: 1}}],
sports: ["baseball", "football", "paddle", "soccer", "boxing", "dart", "biking", "golf", "hockey", "inline-skating", "tennis", "volleyball", "skateboard", "kickball", "bowling", "pool", "ride", "hike", "ice-skating"]
};
this.makeMapEvents = this.makeMapEvents.bind(this) // << important line
}
答案 1 :(得分:2)
.then((response) => {
this.setState({events: resp});
}
您接受参数response
,然后尝试使用resp
,这不是您想要的变量。
答案 2 :(得分:1)
可能原因是您的 func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
if view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
}
else {
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
if view.frame.origin.y != 0 {
self.view.frame.origin.y += keyboardSize.height
}
else {
}
}
}
函数在调用makeMapEvents()
时未提供this
的正确词汇值,因此您的代码无法正常工作。更改this.setState()
函数定义以使用箭头函数(makeMapEvents()
)并提供正确的词法绑定值:
() => {}
您的代码中还有一个拼写错误,其中缺少右括号,如上面的注释所示
答案 3 :(得分:1)
您正在混合组件中的道具和状态。运动应该只在道具中,它不应该处于从父组件传入的状态。
constructor(props) {
(props);
this.state = {
events: [{venue: {lat: 2, lon: 1}}] <== removed sports here, it didn't belong
};
componentWillReceiveProps (nextProps) {
this.setState({events: [{venue: {lat: 2, lon: 1}}],
sports: nextProps.sports});
console.log(nextProps.sports);
}
static defaultProps = {
center: {lat: 36.160338, lng: -86.778780},
zoom: 12,
sports: ["baseball", "football", "paddle", "soccer", "boxing", "dart", "biking", "golf", "hockey", "inline-skating", "tennis", "volleyball", "skateboard", "kickball", "bowling", "pool", "ride", "hike", "ice-skating"],
};
makeMapEvents (insertProps) {
fetch("./json/meetup.json").then((response) => {
return response.json()
}).then((response) => {
/* eventually returns new events object based on insertProps */
this.setState({events: response});
}
};
componentDidMount () {
this.makeMapEvents(this.props.sports); <== changed to props.sports
console.log("mounted", this.props.sports); <== changed to props.sports
}