Comments (14)
@phpfalcon basically, there is a singleton that is imported to all components that are views needing to have "onBack" event. The singleton is named "onBackStack" which is just a simple array.
Each component needing an "onBack" would then push themselves into the onBackStack on their own componentWillMount. On componentWillUnmount, they remove themselves off the stack but then call the method of the last item in the list's onBack() so to fire the event of the component it is going back to. It's kind of like a cooperative chaining of components to accomplish this.
This can be put into a component class then inherited from which is probably a way better idea. I just made it very quickly as I don't care to make it more robust or friendly for what I am doing.
from react-native.
I was able to get a hacked onBack for these types of things using an array as singleton that each component wanting to get there onBack method called would use. The idea is like below.
// singleton onBackStack
export default [];
// example component
class ExampleComponent extends Component {
onBack() {
// do something
}
componentWillMount() {
onBackStack.push(this);
}
componentWillUnmount() {
onBackStack.pop();
if (onBackStack.length) onBackStack[onBackStack.length - 1].onBack();
}
render() {
return <View>example</View>
}
}
from react-native.
@sebringj your method is great but I couldn't understand it until your last comment I implanted a similar method which works for navigatorios :
constructor()
{
super();
this.state = {backapper:false}};
}
componentDidMount() {
var currentRoute = this.props.navigator.navigationContext.currentRoute;
this.props.navigator.navigationContext.addListener('willfocus', (event) => {
//didfocus emit in componentDidMount
if (currentRoute === event.data.route && this.state.backapper) {
console.log("me didAppear");
this.setState({backapper: false});
}
else if (currentRoute !== event.data.route)
{
this.setState({backapper: true});
console.log("me didDisappear, other didAppear");
}
//console.log(event.data.route);
});
}
from react-native.
cc @evv
from react-native.
@ericvicenti actually :p
from react-native.
@vjeux: So far I'm having a great time with react-native. I wrote a tumblr browser that uses the flux pattern heh. This is what it looks like: min-s-react-native-demo-gif. (I'm not inlining the gif because it's too distracting.)
from react-native.
Nice! Glad to hear :)
Hopefully we can get gif support soon so that you can have a fully functional app soon
from react-native.
Unfortunately, the default back button of UINavigationController does not provide an event. The way to get around it is to either set your custom back button on the left side, or to implement - viewWillDisappear:
in iOS. So I guess, this limitation is here to stay.
from react-native.
Since iOS doesn't support it, let's close this for now.
from react-native.
@paramaggarwal is it possible to be notified when any rote changes?
something like
<NavigatorIOS onChange={...}
initialRoute={...} />
I mean when someone do navigator.push(...)
from react-native.
@liubko please look into Navigator
instead, which is much more customisable. Docs are here: Navigator Comparison
from react-native.
i think you can wrap the route component with a wrapper and listen to the wrapper's componentWillUnmount
from react-native.
@sebringj thanks for sharing this can you please explain onBackStack
from react-native.
@phpfalcon Hmm I dont have a navigationContext? Are you using React Navigation? This Google returns no results... navigationContext site:reactnavigation.org
from react-native.
Related Issues (20)
- Sticky views have visual glitches while scrolling in ScrollView HOT 1
- targetSdkVersion 33 to 34 app crash HOT 7
- Error: package com.facebook.react.modules.storage does not exist import com.facebook.react.modules.storage HOT 4
- Heap snapshot throwing `RangeError: Invalid typed array length: -1` on latest chrome version HOT 8
- ☂️ Help us migrate Android tests to AssertJ HOT 23
- Accessibility issue - Android with TalkBack - accessibilityRole="link" is not available when user tries to navigate via links (TalkBack menu)
- App Crash when using ReactNativeFile in apollo upload client HOT 4
- createBundleReleaseJsAndAssets\index.android.bundle:1322:18: warning: the variable "DebuggerInternal" was not declared in function "__shouldPauseOnThrow" typeof DebuggerInternal !== 'undefined' HOT 7
- automaticallyAdjustKeyboardInsets doesn't work in the new arch HOT 2
- App Crashes - On upgrading to Android 14(SDK 34) for "react-native": "0.68.2", HOT 4
- No hot reloading when change font scale
- react-native 0.74.3 new project sync error HOT 3
- Codegen: Int32 generate double on Java HOT 2
- Issue with Top Padding in Text Component on Android (includeFontPadding : false not working🥹) HOT 5
- RTL Layout Direction Not Updating after app reload with New React Native Architecture HOT 10
- Unable to handle hardware back press in Brownfield Setup with BackHandler HOT 5
- App could not Build when trying upgrade sdk 33 to 34 HOT 4
- App could not Build when trying upgrade sdk 33 to 34 (Unexpected error during link, AAPT2 aapt2-4.2.2-7147631-linux Daemon) HOT 4
- react native android crashed in the real device when target API level is changed from 33 to 34. HOT 7
- Upgrade pretty-format to 29 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native.