如何在从另一个页面导航后重新加载页面

6

// 我正在使用以下代码导航到另一个屏幕,即主页。 但是当我导航到主页时,我必须刷新页面...重新加载。当前,在返回主屏幕时,没有调用任何生命周期方法。特别是UserAvatar组件,我必须刷新或重新调用。 请建议

    <View style={{textTransform: 'lowercase'}}><YellowBtn label="Go to 
  Dashboard"
               OnClick={this._redirectCustomerView}/></View>


        _redirectCustomerView = () => {
            this.props.navigation.navigate('UserHome');
          };

//以下是主页

 export default class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = { title: 'Hello!', hasFooterPermission: false };
        console.log("Valuueeeeeee");
      }

      async componentDidMount() {
        const homeFooter = await hasPermission('clm.360D.fe.restrictedView.allowed');

        this.setState({
          hasFooterPermission: homeFooter
        })
      }

      onSearchClick = () => {
        this.props.navigation.navigate('SubscriberSearch');
      };
      componentWillMount(){
        console.log(" Home page dataaaa");
      }


      render() {
        return (
          <View>
            <ImageBackground source={BG} style={{ width: '100%', height: '100%' }} resizeMode="cover">
              <View  style={{ paddingTop: 5 , alignContent:'space-between',flexDirection:'row'}}>
                <View style={{alignSelf: 'flex-start', flex:1}}>
                  <UserAvatar navigationProps={this.props.navigation} avatarSize={40} isTouchable={true}/>
                </View>
                {/* <View style={{alignSelf: 'flex-end'}}>
                  <Icon
                    name="bell-outline"
                    type="MaterialCommunityIcons"
                    style={{ color: '#FFFFFF' }}
                    onPress={() => Toastr.showToast('No new notifications!', 3000)}
                  />
                </View> */}
              </View>
3个回答

18

使用 push 而不是 navigate

this.props.navigation.push('UserHome');

这是正确的答案,谢谢 Shanker。 - ayed abboushi
如何传递参数给这个函数?push('UserHome',{paramaetes}) 不起作用。 - uday

0

主页并不需要知道您是否需要刷新页面。建议的方法是当满足重新加载条件(如头像更新、用户属性更改等)时,调用实体应该进入主页,并在需要时请求重新加载(即window.location.reload(true))。


0
如果您通过导航属性在主页上添加了一个监听器,您可以在转换到主页或从主页转换时调用一个函数('willFocus'/'willBlur'),或者在完成转换后调用一个函数('didFocus'/'didBlur')。这对我很有效:
componentDidMount(){
  this.props.navigation.addListener('willFocus',this.load)
}
load = () => {
  //whatever you want to do when the page loads
}

文档在这里:https://reactnavigation.org/docs/en/navigation-prop.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接