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)
- The Linking does not open an external application HOT 8
- Cannot find module @react-native/codegen/lib/cli/combine/combine-js-to-schema-cli.js HOT 4
- v7 Navigating back randomly causes NAVIGATE action to be dispatched instead of GO_BACK HOT 3
- Bridgeless mode: Reflect, Proxy doesn't exist HOT 1
- .gitignore not provided when doing npx react-native init 'new-project-name' HOT 3
- PhaseScriptExecution [CP-User]\ Generate\ Specs..in target 'React-rncore' from project 'Pods' HOT 1
- Android Crash Thread: SIGQUIT 0x0000000000000000 HOT 5
- 0.73.1 Android flash error message:An error occurred while executing doInBackground() HOT 3
- Execution failed for task ':app:buildCMakeDebug[arm64-v8a]'. Execution failed for task ':app:buildCMakeDebug[x86]' HOT 5
- ✖ Could not create an empty Git repository, see debug logs with --verbose HOT 2
- Pressable interaction issue with StackNavigator on iPhone Xs + new architecture HOT 3
- React-RCTFabric compile issue HOT 1
- How could I init a Fabric component with my customized init method, such as initWithDelegate: HOT 2
- Android View doesnt go to bottom screen edge HOT 2
- Scroll issue in horizontal flatlist in android (react native version 0.73.5) HOT 6
- Can RCTViewComponentView get access to RCTViewManager's bridge property? HOT 4
- on Android,After upgrading React Native from 0.64.2 to 0.72.5, constants returned via getContents in bridge classes cannot be used in JavaScript HOT 9
- Passing gap as a prop to View works even though not supported according to ViewProps / the docs HOT 1
- Animated Value listeners removed after bound elements unmounted
- FlatList scrollToIndex not able to scroll properly for indexes that is in stickyHeaderIndices HOT 4
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.