【问题标题】:Only show redo search button when user has completely moved to a new area仅当用户完全移动到新区域时才显示重做搜索按钮
【发布时间】:2019-06-23 18:04:38
【问题描述】:

如何仅在用户更改区域时才显示重做按钮?在我现在的代码中,它会随着区域的变化而不断闪烁,不确定缺少什么。

代码:

app.js

onregionchange() {
    this.setState({redosearch: !this.state.redosearch })
}

render() {
    const Showredo = ({redosearch }) => redosearch ? <View><Text> redo now <Text></View> : null
    return(
    <View>
    {this.state.redosearch  ? <ShowRedo  redosearch={this.state.redosearch }/> : null}
    <View>
        <MapView
            ref={(map) => (this.map = map)}
            style={styles.map}
            onRegionChange={this.onregionchange}
        >
        </MapView>
    )
}

【问题讨论】:

    标签: reactjs react-native react-native-maps


    【解决方案1】:

    我注意到有几件事可能会导致这里出现问题。首先,看起来您可能正在仔细检查状态值;在 Showredo 元素属性内和返回值内。第二,代码有两个打开的 View 标签和两个没有关闭的 Text 标签。第三,我看不到函数onregionchange是否被绑定。最后,您在渲染函数中返回了两个元素(或者实际上最后缺少两个视图结束标记)

    尝试将您的代码更改为应该更正所有这些的代码:

    onregionchange = () => {
        this.setState({redosearch: !this.state.redosearch })
    }
    
    render() {
        const { redosearch } = this.state;
        return([
        <View key="a_key_for_element_1">
            {redosearch ? <View><Text> redo now </Text></View> : null}
        </View>,
        <MapView 
            key="a_key_for_element_2"
            ref={(map) => (this.map = map)}
            style={styles.map}
            onRegionChange={this.onregionchange}
        >
        </MapView>]
        );
    }
    

    【讨论】:

    • 它仍然像以前一样闪烁。有什么想法吗?
    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    相关资源
    最近更新 更多