如何通过react-navigation触发状态更新?

时间:2019-09-23 14:42:34

标签: ios react-native react-native-navigation react-state-management react-state

在我的React Native应用程序中,我试图更改状态并触发组件的重新渲染。应该在NavBottom调用this.props.navigation.navigate('captureView')导航到CaptureView时完成。状态更新应将CaptureView照片状态变量重设为其原始值。

如何通过react-navigation上的navigate在React Native中更改状态? https://reactnavigation.org/docs/en/navigation-actions.html

CaptureViewCaptureStack

的一部分
import { createStackNavigator } from "react-navigation";

const CaptureStack = createStackNavigator({
    captureView: CaptureView,
    detailView: DetailView,
    reportView: ReportView,

});

const Tab = createBottomTabNavigator({
    capture: CaptureStack,
}, {
    initialRouteName: 'capture',
    tabBarComponent: NavBottom
});

CaptureView.js

import { StackActions, NavigationActions, NavigationEvents } from 'react-navigation';

class CaptureView extends Component {
    static navigationOptions = {}

    constructor(props) {
        super(props);
        console.log("CaptureView: constructor(props)");
    }

    componentDidMount() { // called just once
        console.log("CaptureView: componentDidMount()");
        // this.setState = { // undefined???
        //     photo: null // this needs to be RESET
        // };
    }

    componentWillUnmount() {
        console.log("CaptureView: componentWillUnmount()");
    }

    async onButtonPress() {
        CameraService.takePicture().then((photo)=>{
            this.setState({
                photo: photo
            });

            // More actions

            this.props.navigation.navigate(
                "detailView",
                {
                    id: 'DetailView',
                    photo
                }
            );

        });
    }

    render() {
        return (
            <Camera
                cameraSetter={(cam) => {
                    CameraService.setCamera(cam)
                }}
                photo={this.state.photo}
            />
            <TouchableOpacity onPress={this.onButtonPress.bind(this)}>
                <Text>TAKE PHOTO</Text>
            </TouchableOpacity>
        );
    }
}

然后在应用程序的其他部分,有一个按钮可导航回到CaptureView。

NavBottom.js

export default class NavBottom extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <View>
                <TouchableOpacity onPress={() => this.props.navigation.navigate('captureView')}>
                    <Text>CAMERA</Text>
                </TouchableOpacity>
            </View >);
    }
}

注释

我尝试了与失败的ReactJS(不是React Native)文档不同的方法:

2 个答案:

答案 0 :(得分:2)

您是否尝试过使用导航事件来重置CaptureView组件的状态?

我认为onWillFocus可以解决问题。

https://reactnavigation.org/docs/en/navigation-events.html

答案 1 :(得分:0)

React Navigation不会重新呈现位于Tab Naviagtor中的堆栈中的组件 为了执行强制的重新渲染,您可以像这样使用事件监听器

 componentDidMount() {
        this._navListener = this.props.navigation.addListener('didFocus', () => {
         //perform any action you want, place your logic here
        });

    }

您还可以使用React Navigation的HOC withNavigation ,将( isFocused )道具传递给连接的组件


componentDidUpdate(prevProps, prevState) {
  if(prevProps.isFocused !== this.props.isFocused){
//place your desired logic here
}

}

withNavigation的用法

import { withNavigationFocus } from 'react-navigation';
 ...
 ...
 ...

export default withNavigationFocus(YourComponent)