在子组件中更新/编辑后如何重新渲染父组件React Native?

时间:2018-11-13 05:05:47

标签: javascript reactjs react-native

在更新子组件(ProfileEdit)中的数据后,我在获取父组件(Profile)中的新数据时遇到问题,在这里我创建了一个简单的脚本以便于理解,默认组件是Profile,为什么不能在Profile中显示警报从ProfileEdit返回后,请提供建议或更正我的脚本从ProfileEdit返回后如何显示警报。

Profile.js

export default class Profile extends Component { 
    componentDidMount() {
        alert('Success');
    }

    toProfileEdit() {
        this.props.navigation.navigate('ProfileEdit');  
    }

    render() {
        return (
            <View>
                <Button
                    onPress={() => this.toProfileEdit()}
                    title="Learn More" />
            </View>
        )
    }
}

ProfileEdit.js

export default class ProfileEdit extends Component { 
    backTo() {
        this.props.navigation.navigate('Profile');
    }

    render() {
        return (
            <View>
                <Button
                    onPress={() => this.backTo()}
                    title="Learn More" />
            </View>
        )
    }
} 

请任何人帮助我解决此问题。

谢谢。

1 个答案:

答案 0 :(得分:0)

配置文件已安装,然后componentWillMount将不会在后退操作上再次调用。您可以通过prop操作传递该函数。

 export default class ProfileEdit extends Component { 
  backTo() {
    this.props.navigation.navigate('Profile');
  }
  render() {
    return (
      <View>
        <Button
          onPress={() => { this.props.something(); this.backTo()}}
          title="Learn More" />
      </View>
    )
  }
}

传递功能,该功能的名称为 something (东西),在返回后可以调用它。