加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – React Navigation在返回时重新渲染上一页

发布时间:2020-12-15 20:30:29 所属栏目:百科 来源:网络整理
导读:什么是使用Stack Navigator返回时刷新上一页的最佳方法.在我返回的页面上似乎没有触发生命周期挂钩.我只是使用基本的例子和this.props.navigation.goBack() 解决方法 好的,我在reddit . https://www.reddit.com/r/reactnative/comments/69xm4p/react_navigat
什么是使用Stack Navigator返回时刷新上一页的最佳方法.在我返回的页面上似乎没有触发生命周期挂钩.我只是使用基本的例子和this.props.navigation.goBack()

解决方法

好的,我在reddit . https://www.reddit.com/r/reactnative/comments/69xm4p/react_navigation_tab_change_event/上找到了答案

我使用hooonkos onNavigationStateChange方法使用它粘贴下面创建的示例.我没有拿出这个解决方案所有功劳应该去hooonko.

import React,{ Component } from 'react';
import {
  AppRegistry,StyleSheet,Text,View
} from 'react-native';
import { TabNavigator } from 'react-navigation';
import Icon from 'react-native-vector-icons/FontAwesome';


class MyHomeScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: 'Home',// Note: By default the icon is only shown on iOS. Search the showIcon option below.
    tabBarIcon: ({ tintColor }) => (
      <Icon
        name="home"
        size={30}
        color={tintColor}
      />
    ),};

  _myHomeFunction = () => {
    alert('Here is home tab!');
  }

  componentWillReceiveProps(newProps) {
    if (newProps.screenProps.route_index === 0) {
      this._myHomeFunction();
    }
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.text}>
          Home
        </Text>
      </View>
    );
  }
}

class MyRocketScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: 'Rocket',tabBarIcon: ({ tintColor }) => (
      <Icon
        name="rocket"
        size={30}
        color={tintColor}
      />
    ),};

  _myRocketFunction = () => {
    alert('Here is rocket tab!');
  }

  componentWillReceiveProps(newProps) {
    if (newProps.screenProps.route_index === 1) {
      this._myRocketFunction();
    }
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.text}>
          Rocket
        </Text>
      </View>
    );
  }
}

const MyApp = TabNavigator({
  Home: {
    screen: MyHomeScreen,},Rocket: {
    screen: MyRocketScreen,{
  tabBarOptions: {
    activeTintColor: '#e91e63',});

class rn extends Component {
  _onNavigationStateChange = (prevState,newState) => {
    this.setState({...this.state,route_index: newState.index});
  }
  render() {
    return (
      <MyApp onNavigationStateChange={this._onNavigationStateChange} screenProps={this.state} />
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',text: {
    fontSize: 20,textAlign: 'center',margin: 10,});

AppRegistry.registerComponent('rn',() => rn);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读