te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Override back press react native router flux - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Override back press react native router flux - Stack Overflow

programmeradmin2浏览0评论

I am using react native router flux in my react native app. I want to override the back button press event as I want to add confirmation popup before going back. I have searched a lot but all the links I found was for overriding hardware back button press of Android. I not only want to override the hardware back press but also want to override the back button press event in the navigation bar (for both Android and iOS). Please help me out here.

Edit: I found one way to override the back press event by adding two props in my scene back and onBack. But now problem is I want this backpress to be conditional. I am passing a boolean prop edit to this scene. If the edit is true then only I want to override the backpress otherwise I just want the default one. So how can I change the Scene parameter in my Router.js using the props being passed to that scene?

sudo code

if(edit){
   openConfirmationDialog();
} else {
   Actions.pop();
}

I am using react native router flux in my react native app. I want to override the back button press event as I want to add confirmation popup before going back. I have searched a lot but all the links I found was for overriding hardware back button press of Android. I not only want to override the hardware back press but also want to override the back button press event in the navigation bar (for both Android and iOS). Please help me out here.

Edit: I found one way to override the back press event by adding two props in my scene back and onBack. But now problem is I want this backpress to be conditional. I am passing a boolean prop edit to this scene. If the edit is true then only I want to override the backpress otherwise I just want the default one. So how can I change the Scene parameter in my Router.js using the props being passed to that scene?

sudo code

if(edit){
   openConfirmationDialog();
} else {
   Actions.pop();
}
Share Improve this question edited Jul 27, 2017 at 10:07 Raj Suvariya asked Jul 27, 2017 at 9:40 Raj SuvariyaRaj Suvariya 1,6643 gold badges17 silver badges37 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

To me, the above solution by @Raj Suvariya worked but only if I added a 'renderBackButton' callback to the scene whose 'onBack' I wanted to customize (the 'Home' scene in Raj's example): So this was added to the scene's definition:

renderBackButton={()=>{}}

And this was added upon navigation to the page, same as in Raj's answer above:

Actions.Home({onBack: () => console.log('custom back callback') });

React Native | react-native-router-flux | Redux | Android Back button listener ,handle back press on android while using react native.

// below code works with Android + react native + react-native-router-flux
// this is final index.js file code from where control whole app navigation
import { BackHandler, ToastAndroid } from 'react-native';
import React,{Component} from 'react';
import { Router, Scene, Actions } from 'react-native-router-flux';
import { Provider } from 'react-redux';
// as per your poenents import them accordingly
import Home from './home';
import OtherScreen from './OtherScreen';
//variable 
var backButtonPressedOnceToExit = false;

export default class App extends Component {

  ponentWillMount(){
    BackHandler.addEventListener('hardwareBackPress', this.onBackPress.bind(this));
  }

  ponentWillUnmount(){
    BackHandler.removeEventListener('hardwareBackPress', this.onBackPress.bind(this));
    }

  onBackPress() {
        if (backButtonPressedOnceToExit) {
            BackAndroid.exitApp();
        } else {
            if (Actions.currentScene !== 'Home') {
                Actions.pop();
                return true;
            } else {
                backButtonPressedOnceToExit = true;
                ToastAndroid.show("Press Back Button again to exit",ToastAndroid.SHORT);
                //setting timeout is optional
                setTimeout( () => { backButtonPressedOnceToExit = false }, 2000);
                return true;
            }
        }
    }

    render() {
      return(
        <Provider store={store}>
          <Router backAndroidHandler={this.onBackPress} >
            <Scene key="root" }>
              <Scene key="Home" ponent={Home} initial={true}  />
              <Scene key="OtherScreen" ponent={OtherScreen}  />
            </Scene>
          </Router>
        </Provider>
      );
    }
}

AppRegistry.registerComponent('YourAppNameAccordingToPackageJSON', () => App);

Alright! I found the way to it.

From wherever I am opening this scene I can pass onBack callback like this Actions.Home({onBack: () => console.log('custom back callback') });

This way I can provide dynamic back callback every time I open that scene.

face this problem too, but i did not add onBack in props as Raj post, if you have many places to override ,it will be very annoying to add onBack prop. What i do is to override RNRF's default onBackPress function.

//App.js

    <Router
        scenes={scenes}
        createReducer={reducerCreate}
        getSceneStyle={getSceneStyle}
        backAndroidHandler={() => {
            let cs = Actions.currentScene;
            if (cs === 'sc1' || cs === 'sc2') {

            } else {
                // default, pop back
                Actions.pop();
            }
            return true;
        }}
    />
    //ponent1.js, "ponent1" is a key in scenes registered.

    handleBackAction = () => {
        if (Actions.currentScene === "ponent1") {
            // do something you want and return true to intercept back event.
            return true;
        }
        return false;
    }
    ponentDidMount() {
        this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackAction);
    }

    ponentWillUnmount() {
        this.backHandler.remove();
    }
发布评论

评论列表(0)

  1. 暂无评论