【问题标题】:React Native component life cycle - which function gets called when screen is visibleReact Native 组件生命周期 - 当屏幕可见时调用哪个函数
【发布时间】:2018-02-23 22:48:39
【问题描述】:

我一直在查看https://reactjs.org/docs/react-component.html 此处的文档,但有些东西一直困扰着我。一种我几乎经常需要的模式,但我无法找到解决方案,因此总是不得不在它周围找到黑客。

我所说的模式如下。我的应用程序有一个 TabNavigator,我知道当应用程序初始化时,所有选项卡上都会调用 ComponentDidMount。我想要的是在使用this.props.navigation.navigate('TAB1') 导航到选项卡或在屏幕底部单击选项卡时调用的函数。

如果有人可以提供帮助,我将不胜感激。抱歉,没有可显示的代码。

谢谢

【问题讨论】:

    标签: react-native components lifecycle


    【解决方案1】:

    首先,要了解它不那么容易的原因,请阅读相应问题中的以下对话:https://github.com/react-navigation/react-navigation/issues/51

    这是目前看起来最有效的解决方案:https://github.com/react-navigation/react-navigation/pull/3345

    这是您可以尝试的示例代码:

    import type {
      NavigationScreenProp,
      NavigationEventSubscription,
    } from 'react-navigation';
    
    import React from 'react';
    import { Button, Platform, ScrollView, StatusBar, View } from 'react-native';
    import { SafeAreaView, TabNavigator } from 'react-navigation';
    import Ionicons from 'react-native-vector-icons/Ionicons';
    import SampleText from './SampleText';
    
    const MyNavScreen = ({ navigation, banner }) => (
      <SafeAreaView forceInset={{ horizontal: 'always', top: 'always' }}>
        <SampleText>{banner}</SampleText>
        <Button
          onPress={() => navigation.navigate('Home')}
          title="Go to home tab"
        />
        <Button
          onPress={() => navigation.navigate('Settings')}
          title="Go to settings tab"
        />
        <Button onPress={() => navigation.goBack(null)} title="Go back" />
        <StatusBar barStyle="default" />
      </SafeAreaView>
    );
    
    const MyHomeScreen = ({ navigation }) => (
      <MyNavScreen banner="Home Tab" navigation={navigation} />
    );
    
    MyHomeScreen.navigationOptions = {
      tabBarTestIDProps: {
        testID: 'TEST_ID_HOME',
        accessibilityLabel: 'TEST_ID_HOME_ACLBL',
      },
      tabBarLabel: 'Home',
      tabBarIcon: ({ tintColor, focused }) => (
        <Ionicons
          name={focused ? 'ios-home' : 'ios-home-outline'}
          size={26}
          style={{ color: tintColor }}
        />
      ),
    };
    
    type MyPeopleScreenProps = {
      navigation: NavigationScreenProp<*>,
    };
    class MyPeopleScreen extends React.Component<MyPeopleScreenProps> {
      _s0: NavigationEventSubscription;
      _s1: NavigationEventSubscription;
      _s2: NavigationEventSubscription;
      _s3: NavigationEventSubscription;
    
      static navigationOptions = {
        tabBarLabel: 'People',
        tabBarIcon: ({ tintColor, focused }) => (
          <Ionicons
            name={focused ? 'ios-people' : 'ios-people-outline'}
            size={26}
            style={{ color: tintColor }}
          />
        ),
      };
      componentDidMount() {
        this._s0 = this.props.navigation.addListener('willFocus', this._onEvent);
        this._s1 = this.props.navigation.addListener('didFocus', this._onEvent);
        this._s2 = this.props.navigation.addListener('willBlur', this._onEvent);
        this._s3 = this.props.navigation.addListener('didBlur', this._onEvent);
      }
      componentWillUnmount() {
        this._s0.remove();
        this._s1.remove();
        this._s2.remove();
        this._s3.remove();
      }
      _onEvent = a => {
        console.log('EVENT ON PEOPLE TAB', a.type, a);
      };
      render() {
        const { navigation } = this.props;
        return <MyNavScreen banner="People Tab" navigation={navigation} />;
      }
    }
    
    type MyChatScreenProps = {
      navigation: NavigationScreenProp<*>,
    };
    class MyChatScreen extends React.Component<MyChatScreenProps> {
      _s0: NavigationEventSubscription;
      _s1: NavigationEventSubscription;
      _s2: NavigationEventSubscription;
      _s3: NavigationEventSubscription;
    
      static navigationOptions = {
        tabBarLabel: 'Chat',
        tabBarIcon: ({ tintColor, focused }) => (
          <Ionicons
            name={focused ? 'ios-chatboxes' : 'ios-chatboxes-outline'}
            size={26}
            style={{ color: tintColor }}
          />
        ),
      };
      componentDidMount() {
        this._s0 = this.props.navigation.addListener('willFocus', this._onEvent);
        this._s1 = this.props.navigation.addListener('didFocus', this._onEvent);
        this._s2 = this.props.navigation.addListener('willBlur', this._onEvent);
        this._s3 = this.props.navigation.addListener('didBlur', this._onEvent);
      }
      componentWillUnmount() {
        this._s0.remove();
        this._s1.remove();
        this._s2.remove();
        this._s3.remove();
      }
      _onEvent = a => {
        console.log('EVENT ON CHAT TAB', a.type, a);
      };
      render() {
        const { navigation } = this.props;
        return <MyNavScreen banner="Chat Tab" navigation={navigation} />;
      }
    }
    
    const MySettingsScreen = ({ navigation }) => (
      <MyNavScreen banner="Settings Tab" navigation={navigation} />
    );
    
    MySettingsScreen.navigationOptions = {
      tabBarLabel: 'Settings',
      tabBarIcon: ({ tintColor, focused }) => (
        <Ionicons
          name={focused ? 'ios-settings' : 'ios-settings-outline'}
          size={26}
          style={{ color: tintColor }}
        />
      ),
    };
    
    const SimpleTabs = TabNavigator(
      {
        Home: {
          screen: MyHomeScreen,
          path: '',
        },
        People: {
          screen: MyPeopleScreen,
          path: 'cart',
        },
        Chat: {
          screen: MyChatScreen,
          path: 'chat',
        },
        Settings: {
          screen: MySettingsScreen,
          path: 'settings',
        },
      },
      {
        lazy: true,
        removeClippedSubviews: true,
        tabBarOptions: {
          activeTintColor: Platform.OS === 'ios' ? '#e91e63' : '#fff',
        },
      }
    );
    
    type SimpleTabsContainerProps = {
      navigation: NavigationScreenProp<*>,
    };
    
    class SimpleTabsContainer extends React.Component<SimpleTabsContainerProps> {
      static router = SimpleTabs.router;
      _s0: NavigationEventSubscription;
      _s1: NavigationEventSubscription;
      _s2: NavigationEventSubscription;
      _s3: NavigationEventSubscription;
    
      componentDidMount() {
        this._s0 = this.props.navigation.addListener('willFocus', this._onAction);
        this._s1 = this.props.navigation.addListener('didFocus', this._onAction);
        this._s2 = this.props.navigation.addListener('willBlur', this._onAction);
        this._s3 = this.props.navigation.addListener('didBlur', this._onAction);
      }
      componentWillUnmount() {
        this._s0.remove();
        this._s1.remove();
        this._s2.remove();
        this._s3.remove();
      }
      _onAction = a => {
        console.log('TABS EVENT', a.type, a);
      };
      render() {
        return <SimpleTabs navigation={this.props.navigation} />;
      }
    }
    
    export default SimpleTabsContainer;
    

    源码和其他代码看这里:https://github.com/react-navigation/react-navigation/blob/master/examples/NavigationPlayground/js/SimpleTabs.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      相关资源
      最近更新 更多