Comments (2)
I figured it out how to implement the functionality which I need using the current version of "react navigation". So, nevermind. :)
Here is what I did:
NavigationService.js:
import { NavigationActions, StackActions } from 'react-navigation';
let _navigator = null;
let _defaultGetStateForAction = null;
let _getStateForAction = (action, state) => {
if (state && action.type === NavigationActions.BACK) {
let _params = _getCurrentRoute(state).params;
if (_params && _params.onBackAction && _params.onBackAction()) {
return null; // action has been taken care of already
}
}
return _defaultGetStateForAction(action, state);
};
function _getCurrentRoute(state) {
while (state.routes) {
state = state.routes[state.index];
}
return state;
}
function back() {
_navigator.dispatch(
NavigationActions.back({
key: _getCurrentRoute(_navigator.state.nav).key,
})
);
}
function navigate(routeName, params = {}) {
_navigator.dispatch(
StackActions.push({
routeName,
params,
})
);
}
function navigateHome() {
_navigator.dispatch(
StackActions.popToTop()
);
}
function setTopLevelNavigator(navigatorRef) {
if (navigatorRef && _navigator === null) {
_navigator = navigatorRef;
let router = _navigator._navigation.router;
_defaultGetStateForAction = router.getStateForAction;
router.getStateForAction = _getStateForAction;
}
}
export default {
back,
navigate,
navigateHome,
setTopLevelNavigator,
};
DetailsScreen.js:
import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';
import nav from '../utils/NavigationService';
export default class DetailsScreen extends Component {
static navigationOptions = {
title: 'Details',
};
state = {
itemId: null
};
stateHistory = [];
_onBackAction = () => {
if (this.stateHistory.length === 0)
return false;
let _newState = this.stateHistory.pop();
this.setState(_newState);
return true;
}
_updateState = (_newState) => {
this.stateHistory.push({...this.state});
this.setState(_newState);
}
componentDidMount() {
const { navigation } = this.props;
navigation.setParams({
onBackAction: this._onBackAction
});
const itemId = navigation.getParam('itemId', 0);
this.setState({
itemId
});
}
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Text>itemId: {this.state.itemId}</Text>
<Button
title="Go to Home"
onPress={() => nav.navigateHome()}
/>
<Button
title="Go to Details (again...)"
onPress={() => nav.navigate('Details', {
itemId: Math.floor(Math.random() * 100),
})}
/>
<Button
title="Update itemId"
onPress={() => this._updateState({
itemId: Math.floor(Math.random() * 100),
})}
/>
<Button
title="Go to Another"
onPress={() => nav.navigate('Another')}
/>
<Button
title="Go back"
onPress={() => nav.back()}
/>
</View>
);
}
}
from rfcs.
Closing since this repo is for proposals, not questions.
from rfcs.
Related Issues (20)
- Action property that indicates to routes that they should not change index HOT 1
- Improve ergonomics of back HOT 1
- Navigator config to pass params down HOT 1
- Add unsetParams to navigation prop HOT 2
- Reset state action HOT 2
- Expose logic for initializing state of createAppContainer HOT 4
- On will/didFocus subscribe, stop firing the listener if current screen is focused HOT 2
- Idea: Preloading HOT 6
- <StaticNavigator> for tests/storybook HOT 14
- StackActions.popToRouteName(routeName) HOT 2
- Alternative API for defining navigators HOT 12
- how to send navigation events between peer navigators? HOT 4
- Deep linking with authentication
- How to hide tab bar item n react-navigation HOT 3
- Ability to show screens on top of native modals
- useNavigationParams hook HOT 1
- Add useMaterialTabsHeight HOT 1
- Typesafe Stack & Navigation HOT 1
- devTools prop HOT 5
- [RFC] TabView API change
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 rfcs.