代码之家  ›  专栏  ›  技术社区  ›  TheFlyBiker 420

改变谷歌地图的主题点击反应本机

  •  1
  • TheFlyBiker 420  · 技术社区  · 6 年前

    this.state.mapTheme .

    constructor() {
        super();
        this.state = {
            mapTheme: Themes.SANANDREAS
        }
    }
    

    render() {
        return (
            <Container style ={styles.container}>      
                <Button rounded style={styles.contributeButton} onPress={() => {
                    this.setState({
                        mapTheme: Themes.BROWNTHEME
                    })
                }} iconLeft light>
                  <Icon name='hand' />
                  <Text>Contribute</Text>
                </Button>
                <MapView
                    style={styles.map}
                    provider = { MapView.PROVIDER_GOOGLE }
                    customMapStyle = { this.state.mapTheme }
                ></MapView>
            </Container>
    );}
    

    这一切似乎都在工作,但当我改变按下按钮的状态,渲染方法被调用,但主题 没有改变。我的两个主题都在发挥作用,但在媒体上什么也没发生。重新加载页面,但不使用新主题。我做错什么了?

    1 回复  |  直到 6 年前
        1
  •  2
  •   gazdagergo    6 年前

    MapView 组件的生命周期功能如果传递了新的样式道具,它不会自动更新样式。你应该通过打电话来强迫它 _updateStyle

    setMapRef = ref => this.mapRef = ref;
    
    handleButtonClick = () => {
      this.setState({ mapTheme: Themes.BROWNTHEME }, () => {
        this.mapRef._updateStyle();
      })
    }
    
    render() {
      return (
         <Container>      
           <Button onPress={this.handleButtonClick}
             title="Update map theme"
           />
           <MapView
             ref={this.setMapRef}
             customMapStyle={this.state.mapTheme}
           />
         </Container>
      );
    }