New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reset action does not seem to work #59
Comments
Hey Mark, I got the same problem on my end so I'll let you know if I come up with a solution. |
Ok So I have a solution: You need to add an index of 0 to your reset action: Haven't yet figured out how to run this only at the end of a transition - think you may need to use the Transitioner component |
Awesome, that works! Maybe it would be useful to have a 'reset' function on the 'navigation' object? |
@JustinNothling, good point, we don't expose Transitioner's I'm on the fence about adding a |
@ericvicenti we're in agreement about the How would you go about structuring a simple login/logout using react-navigation? At the moment pushing a home route onto login allows users to |
You might fine this useful. It is a variation of the tutorial where I have a login screen and then a logout function under the Options right element of the RecentChatsScreen which will get you back to the original login screen. import React from 'react'; class SigninScreen extends React.Component { class OptionsScreen extends React.Component { class RecentChatsScreen extends React.Component {
}; render() { class AllContactsScreen extends React.Component { } const MainScreenNavigator = TabNavigator({ MainScreenNavigator.navigationOptions = { class ChatScreen extends React.Component { const TheApp = StackNavigator({ }); AppRegistry.registerComponent('exp', () => TheApp); |
@JustinNothling, I would probably want to prevent the gesture. But I don't think there is a way to do that currently. It might make sense to add a |
I have tried to implement Login/Logout using Reset action. However, with NestedNavigators I get the following error:
I run the redux implemantation, like in the docs, so in a middleware, I run: // middleware
if (prevState.isAuthenticated !== nextState.isAuthenticated) {
const nextScreen = nextState.isAuthenticated
? 'Home'
: 'Authenticate'
dispatch({
type: 'Reset',
index: 0,
actions: [{
type: 'Navigate',
routeName: nextScreen,
}],
})
} Here is my reduced Navigator config: // Navigator settings
const AuthenticateNavigator = StackNavigator({
FacebookAuthentication: { screen: FacebookAuthScreen },
EmailAuthentication: { screen: EmailAuthScreen },
})
const MainScreenNavigator = TabNavigator({
Recent: { screen: RecentScreen },
Logout: { screen: LogoutScreen },
})
const AppNavigator = StackNavigator({
Authenticate: { screen: AuthenticateNavigator },
Home: { screen: MainScreenNavigator },
}) |
Actually, the reset action doesn't seem to work properly. If I reset to 'Home' it works, but if I add additional actions to the reset it doesn't navigate to the right screen and seems to "freeze" (no button presses get registered). This is the code: dispatch({
type: 'Reset',
index: 0,
actions: [
{
type: 'Navigate',
routeName: 'Home',
},
{
type: 'Navigate',
routeName: 'Items',
},
],
}) This is the state: { action:
{ type: 'Reset',
index: 0,
actions:
[ { type: 'Navigate', routeName: 'Home' },
{ type: 'Navigate', routeName: 'Items' } ] },
newState:
{ index: 0,
routes:
[ { routeName: 'Home', key: 'Init0' },
{ routeName: 'Items', key: 'Init1' } ] },
lastState:
{ index: 3,
routes:
[ { routeName: 'Home', key: 'Init' },
{ type: 'Navigate',
routeName: 'Items',
params: undefined,
action: undefined,
key: 'id-1485774310007-0' },
{ type: 'Navigate',
routeName: 'Menu',
params: undefined,
action: undefined,
key: 'id-1485774310007-1' },
{ type: 'Navigate',
routeName: 'Checkout',
params: undefined,
action: undefined,
key: 'id-1485774310007-2' } ] } } |
The workaround that works for me is to set up deep linking and use Linking.openURL(...). However, this will only work well if you are happy with a navigation stack of 1 level deep (where the back arrow points back to home). |
I fixed my issue by restraining myself to only one level of depth in my navigation, without deep linking. const MainScreenNavigator = TabNavigator({
Recent: { screen: RecentScreen },
Logout: { screen: LogoutScreen },
})
const AppNavigator = StackNavigator({
FacebookAuthentication: { screen: FacebookAuthScreen },
EmailAuthentication: { screen: EmailAuthScreen },
Home: { screen: MainScreenNavigator },
}) |
Maybe I'm misunderstanding, but shouldn't it be possible to simply dispatch a goBack, if you want a simple pop? Or do we need to manually dispatch reset actions in this case? |
@lslima91 a -> At least i think this is the functionality of ps: I am also running into this issue, it looks like a second action cannot be sent to the child router. |
@elliottjro The thing is, I'm not able to dispatch a type: goBack action. I have to manually dispatch a reset with the correct routename. |
nvm, I have to dispatch {type:'Back'}, this new PR makes things much better imo #120 |
Hey has anyone got the new code on for reset in the docs to work?
I'm getting an error saying reset is undefined. |
@JustinNothling are using the version from master? |
@JustinNothling I have, but I had to tweak a bit the code... @satya164 I'm on master and by reading the docs it seems that for dispatching a reset action I need all I have to do is the following: const actionToDispatch = NavigationActions.reset({
actions: NavigationActions.navigate({ routeName })
})
this.props.navigation.dispatch(actionToDispatch) Which doesn't work. const actionToDispatch = NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName })] // Array!
})
this.props.navigation.dispatch(actionToDispatch) Is this the correct way to reset the stack? Am I just misunderstanding the docs? Let me know, I'd like to PR with a "clear way" for resetting the stack. |
the second one is correct. seems there's an error in the doc. willing to send a PR? |
@mmazzarolo, |
Copy-paste from #156@rohit-ravikoti the snippet you pasted is not using redux, it is just plain Make sure you're using the
Let me know if it works :) |
That worked! Thanks! 😜 |
- Added the index - Actions is now an array Related to #59 (comment)
Hey all, it looks like this issue is fixed but original issue was using the reset action along with a nested navigation action. @markflorisson can you confirm this is the case else close current issue. |
Hi @elliottjro, yeah the refactor looks great but nested navigation actions still don't seem to work: this.props.navigation.navigate(
NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName: 'Home',
}),
NavigationActions.navigate({
routeName: 'Items',
params: {
...
},
}),
],
})
) |
Or do we have to nest the second 'navigate' action inside the first one, like so? this.props.navigation.navigate(
NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName: 'Home',
actions: [
NavigationActions.navigate({
routeName: 'Items',
params: {
...
},
}),
]
}),
],
})
) |
BTW the most reliable way to work around is it to navigate to the 'Home' screen with a param, and then to do a 'navigate' from the Home screen to whereever you need to redirect. But it's a hassle and it involves an unnecessary rendering of the home screen. |
@markflorisson, it can be hard to see what is going wrong exactly. Can you write a test for |
@markflorisson - do you have an example of navigation with the param? I am stuck in the way at the moment |
@Paul-Todd hi Paul, this is the code I use in my Home screen: class HomeScreen extends Component {
redirectKey = null
render = () => {
const { redirectRoute, redirectKey, redirectParams } = this.params
if (redirectRoute && redirectKey != this.redirectKey) {
this.redirectKey = redirectKey
setTimeout(() => {
this.props.navigation.navigate(redirectRoute, redirectParams)
}, 0)
}
return (
<HomeView
navigation={this.props.navigation}
/>
)
}
} Putting the redirect logic in componentDidMount doesn't work, as componentDidMount is only called once on the home screen (not sure when it is unmounted). From another page I then reset to the home screen as follows: import { NavigationActions } from 'react-navigation'
import shortid from 'shortid'
const resetRoute = (navigation, params) => {
navigation.dispatch(
NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName: 'Home',
params: params,
}),
],
}),
)
}
class SomeOtherScreen extends Component {
handlePress = () => {
resetRoute(this.props.navigation, {
redirectRoute: 'ScreenToRedirectTo',
redirectKey: shortid(),
redirectParams: {
... params for ScreenToRedirectTo ...
},
})
}
...
} NOTE: the Disclaimer: This code is pretty terrible, I don't recommend using it if you can avoid it :) |
@ericvicenti Will give it a go, thanks! |
Ok so the issue is not necessarily with the reducers so much as how to use the reset action. What you need to do is make sure you set the correct index. This works for me: this.props.navigation.dispatch(
NavigationActions.reset({
index: 1,
actions: [
NavigationActions.navigate({
routeName: 'Home',
}),
NavigationActions.navigate({
routeName: 'Bar',
params: {
...,
},
}),
],
}),
) Setting the 'index' as part of the 'navigate' doesn't seem to work. |
(BTW: What is the reason for 'index'? Can the reducer not just take 'actions.length'?) |
Theoretically, I think index should be optional and would default to Closing this out because it seems like everything is working. And I'll continue to think about how the API could be improved. |
@CoteViande This solved ALL my navigation problems! |
- Added the index - Actions is now an array Related to react-navigation/react-navigation#59 (comment)
By shipping source files and setting the ‘react-native’ entry point to src, the RN packager can parse the module and provide source maps when debugging from your app. This makes the install slightly heavier but is worth it for the improved ergonomics.
- Added the index - Actions is now an array Related to react-navigation#59 (comment)
By shipping source files and setting the ‘react-native’ entry point to src, the RN packager can parse the module and provide source maps when debugging from your app. This makes the install slightly heavier but is worth it for the improved ergonomics.
- Added the index - Actions is now an array Related to react-navigation/react-navigation#59 (comment)
I'm trying to reset the stack in a StackNavigation using:
However, this gives an error: "There should always be only one scene active, not 0".
If I try to dispatch() the reset action, followed by a navigate(), then it works but it does not "forget" any part of the history stack. How can I reset the stack?
The text was updated successfully, but these errors were encountered: