Using react native device back API:
Here we discuss “How to handle device back button in react-native with react-native-router-flex“, in iOS there is no back button so no need to handle this but in android device back button is a big issue, for handling the device back button react-native provide a API.
I’m a big fan of React Native, because it is very easy to implement complicated concepts and designs, in below example i am going to explain you in detail how to handle device back button.
Output is like –
so let’s see how we handle this.
Follow these steps for handling android back button in react native-
Step 1:
import the following line.
import { BackHandler } from ‘react-native’;
Step 2:
Declare it on constructor so we find it only one time.
constructor(props) {
super(props)
this.handleBackButtonClick = this.handleBackButtonClick.bind(this);
}
Step 3:
Now we call it on ComponentDidMount().
componentWillMount() {
BackHandler.addEventListener(‘hardwareBackPress’,
this.handleBackButtonClick);
}
Step 4:
When we leave that page then we need to unsubscribe the API, so we put that code within unmount lifecycle of react native.
componentWillUnmount() {
BackHandler.removeEventListener(‘hardwareBackPress’,
this.handleBackButtonClick);
}
Step 5:
Now we define the function.
handleBackButtonClick() {
Actions.sceneName();//such as: home(),login() etc
}
I’m a big fan of React Native, because it is very easy to implement complicated concepts and designs, in above example, explain you in detail how to handle device back in a very simple way.
So we completed the topic “How to handle device back button in react-native with react-native-router-flex“.
You can find my post on medium as well click here please follow me on medium as well.
You can find my next post here.
If have any query/issue, please feel free to ask.
Happy Coding Guys.
Hi, I am a professional Ionic and React Native Pixel Perfect App Designer and Developer, with expertise in Client Communication, Bug Fixing, Third Party Lib, Version Control Tools, Requirement Understanding, and managing teams, I have 6+ years of experience in the same domain as well as in Codeigniter, JS, IoT, and more than 10 other languages. For the last 6+ years, not a single day went without design/development.
Please follow me on Medium: https://nehadwivedi1004.medium.com/
back handler add in step 5, follow step 5 in this blog if it not solved then add a screenshot of your project
and how to achive .. you write Action its texts how to get .
my code give me error
App.js in my home screen is import .. “bottom tab bar” .
name is = tabnavigator
where you code add back handler .
give the name of that tab navigate screen, where you handled the back button
Hi my name Nilesh
How to bottom tab bar in add back handler PLZZ suggest me
My main screen is tab navigator