【问题标题】:React Native - Why Component inside TabNavigator Not get Called againReact Native - 为什么 TabNavigator 中的组件不再被调用
【发布时间】:2018-03-21 10:31:09
【问题描述】:

我在 react native 中遇到了一个问题,但还没有得到任何解决方案。 我使用 TabNavigator。当我单击一个选项卡时,它将成功调用其中的组件。但是当我通过离开组件离开另一个选项卡,然后返回上一个选项卡时,数据仍然存在/存在。它根本不会重新渲染。 如何在 TabNavigator 中重新加载组件?我尝试使用 redux 并在组件类中实现 componentDidUpdate 但它根本没有被调用,似乎这不是问题。它只是没有被第二次调用的组件类 请帮忙。

我的 TabNavigator 代码:

const AppTabNav = TabNavigator({
Home: {
    screen: AppStack,
    navigationOptions: {
        fontSize: 4,
    }
},
Notification: {
    screen: RequestStack,
    navigationOptions: {
        // headerTitle: 'HRIS',
        // label: 'HRIS',
        fontSize: 4
    }
},
Request222: {
    screen: NotifStack,
    navigationOptions: {
        headerTitle: 'HRIS',
        label: 'HRIS',
        fontSize: 4
    }
},},

{
    tabBarPosition: 'bottom',
    animationEnabled: true,
    swipeEnabled: true,
    tabBarOptions: {
        activeTintColor: '#022846',
        // inactiveBackgroundColor: '#89898b',
        inactiveTintColor: '#89898b',
        upperCaseLabel: false,
        // activeBackgroundColor:'#fff',
        showLabel: true,
        showIcon: true,
        // style: styles.tabBar,
        style: {
            backgroundColor: '#eaeaec'
        },
    },     
});

【问题讨论】:

    标签: android reactjs react-native react-navigation


    【解决方案1】:

    TabNavigator 中的屏幕只会渲染一次,除非屏幕的任何道具或状态值发生更改。这是默认和预期的行为。

    如果您在重新访问时需要重新渲染屏幕,您可以使用addListener 来监听焦点事件并相应地更改该屏幕所需的值(示例:做一个fetch从服务器获取数据)

    addListener - 订阅导航生命周期的更新

    React Navigation 向订阅它们的屏幕组件发出事件:

    • willBlur - 屏幕将失焦
    • willFocus - 屏幕将聚焦
    • didFocus - 屏幕聚焦(如果有过渡,则过渡完成)
    • didBlur - 屏幕失焦(如果有过渡,则过渡完成)

    来自文档的示例

    const didBlurSubscription = this.props.navigation.addListener(
      'didBlur',
      payload => {
        console.debug('didBlur', payload);
      }
    );
    
    // Remove the listener when you are done
    didBlurSubscription.remove();
    
    // Payload
    
    {
      action: { type: 'Navigation/COMPLETE_TRANSITION', key: 'StackRouterRoot' },
      context: 'id-1518521010538-2:Navigation/COMPLETE_TRANSITION_Root',
      lastState: undefined,
      state: undefined,
      type: 'didBlur',
    };
    

    【讨论】:

      【解决方案2】:

      这就是 tabnavigator 的工作原理。事实上,默认情况下,所有选项卡都会在初始加载时呈现。

      对于组件重新加载,这可能会有所帮助。 (React Native reload page)

      【讨论】:

        猜你喜欢
        • 2018-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-17
        • 1970-01-01
        • 1970-01-01
        • 2017-05-21
        • 1970-01-01
        相关资源
        最近更新 更多