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

时间:2019-05-24 04:52:20

标签: javascript reactjs react-native react-redux

///我正在使用以下代码将一个屏幕导航到另一个屏幕,即主页。 但是在浏览主页时,我必须刷新页面..reload。目前,当我回到主屏幕时,没有生命周期方法被呼叫。特别是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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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

答案 2 :(得分:0)

使用推送而不是导航

this.props.navigation.push('UserHome');
相关问题