【问题标题】:React Navigation re-render previous page when going back返回时 React Navigation 重新渲染上一页
【发布时间】:2025-12-17 21:45:02
【问题描述】:

使用 Stack Navigator 返回时刷新上一页的最佳方式是什么。我返回的页面上似乎没有触发任何生命周期挂钩。我只是使用基本示例和this.props.navigation.goBack()

【问题讨论】:

  • 您在返回之前是否更改了上一页的任何状态/道具?如果您已更改,则上一页应自动重新呈现。
  • 它是一个正在更改的本地数据库值,对上一页有影响。所以我需要它来检查数据库中的任何值变化。

标签: react-native react-navigation


【解决方案1】:

您可以为此使用addListener

componentDidMount() {

    this.props.navigation.addListener(
      'didFocus',
      payload => {
        this.forceUpdate();
      }
    );

}

【讨论】:

【解决方案2】:

在反应功能组件中你可以这样做:

const SomeComponent = (props) => {

useEffect(() => {
        props.navigation.addListener(
            'didFocus',
            payload => {
                //call some action here
            }
        );
    }, [])
}

每当您登陆组件时,上述效果就会触发,只要您从导航堆栈返回并且您想要触发 api 或进行一些计算,它就会起作用。

【讨论】:

    【解决方案3】:

    好的,我在 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);
    

    【讨论】:

    • componentWillReceiveProps 已被弃用。
    • @cmcodes 是的,从现在开始应该使用 getDerivedStateFromProps
    【解决方案4】:

    这对我有用:

    const SomeComponent = (props) => {
    
        useEffect(() => {
            props.navigation.addListener(
                'focus',
                payload => {
                    // call some action here
                }
            );
        }, [])
    
    }

    只要导航聚焦在组件上,它就会执行。

    对于类组件,这应该可以工作:

    componentDidMount() {
    
      this.props.navigation.addListener(
        'focus',
        payload => {
          // call some action here
        }
      );
    
    }
    

    【讨论】: