A → B
使用 navigator 導航,用 goBack() 返回的時候傳遞參數
在 A 頁面
this.props.navigation.navigate("B", { callBack: (data) => { //回調函數
this.setState({ studentCount: data }) } });
在 B 頁面
onPress()=>{ this.props.navigation.state.params.callBack(data) this.props.navigation.goBack(); }
在使用 Android 機的時候,點擊物理返回鍵的時候,並不會走 navigator ,所以需要加一個返回鍵的事件監聽
1.在生命周期中監聽
componentDidMount() { BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid); } componentWillUnmount() { BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid); } onBackAndroid = () => { this.props.navigation.goBack(); this.props.navigation.state.params.callBack(data) return true }
2.待測試
