Skip to content
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

Do you store navigation stack in state? #393

Closed
wojciech-kulik opened this issue Mar 6, 2019 · 6 comments
Closed

Do you store navigation stack in state? #393

wojciech-kulik opened this issue Mar 6, 2019 · 6 comments

Comments

@wojciech-kulik
Copy link

Hi,

I've got a few questions:

  1. Do you guys store navigation stack in state?
  2. Or in general, how do you deal with navigation on iOS using Redux?
  3. Do you have a possibility to restore whole navigation stack when state changes?
  4. What is recommended approach in Redux?

Redux was invented for web (js) where as far as I know navigation is either similar to starting up whole application (loading a different URL) or changing visibility in a single HTML source. In contrast to mobile apps where you need to restore whole complex navigation stack like tabs, nested navigation controllers, modal views etc.

I don't think that in many applications it would be beneficial to be able to restore whole navigation, but I wonder what is your way to deal with it?

Best regards,
Wojciech Kulik

@dani-mp
Copy link
Contributor

dani-mp commented Mar 6, 2019

You can totally do that. Managing the navigation of the app with ReSwift doesn't seem super complicated, but I don't know how people have solved the problem with pressing/swiping back, for instance, because that's something that's given for free and it's not tied to a programmatic action you explicitly perform. Same thing with dismissing alerts/popovers, etc. In those cases, it's not that you update the view because your state has changed, but rather the view changes by itself and you would need to, at least, catch those events and sync back your ReSwift state to keep it consistent.

Regarding restoring the state of your app (not talking about ReSwift here), Apple already provides something for this, you can check this. This is fairly easy to integrate, but in order to keep in sync both the app and your ReSwift state, you will probably have to serialise and persist it yourself, so once the app has been restored, you can continue managing its navigation with ReSwift where you left it. This seems trickier but doable.

@wojciech-kulik
Copy link
Author

wojciech-kulik commented Mar 6, 2019

Thank you @danielmartinprieto for your answer :). Good catch with back navigation.

To be clear, I don't want to implement this. I'm just curious if it's common for people using Redux on iOS to keep navigation in state and use Actions to push/pop ViewController.

Personally, I use Redux for navigation only partially. For example I have a hamburger menu with SideMenuViewController and when user selects an item, I dispatch SelectScreenAction and MainViewController replaces its ViewController based on new state.
However, my app is not able to restore whole navigation stack and I don't think I need it.

I know that there is also ReSwift-Router library to help with navigation. As you mentioned, it's doable for sure, but to me it looks like a lot of effort with questionable benefits, but probably to be 100% Redux, we shoud keep navigation in state, because after all it's a shared state of the application.

@dani-mp
Copy link
Contributor

dani-mp commented Mar 6, 2019

Yeah, totally agree that it's much easier for a web app, where routing is done these days in a fairly declarative manner, which fits Redux/ReSwift better.

@Dimillian
Copy link

Dimillian commented Mar 12, 2019

I have a UIState, it tracks some stuff, but not the whole navigation of my app.
For example I have my selected tab of my UITabbarController in it, and it's sync with the controller. So in order to change tab, all I do is store.dispatch(UIActions.SetSelectedTab(tab: .home)).

Initially I was tracking a lot more things, like a modal stack, so you could present a modal with an Action, and dismiss all thought actions. But it become quite a mess, so I went back to present them locally and I don't track the state in the store anymore. I have a very basic routeur which can push a ViewController on the top most UINavigationController from dispatching an action, but even that seems a bit messy.

But I use it extensively for my custom UI stuff, like to present my custom bottom sheet, show a notification etc.. it's all done trough actions and reducers, and it's very convenient. I also track the device orientation, and subscribe to it whenever needed. And a bunch of other stuff.

@brownsoo
Copy link

I store some UI states using ReSwift. - ( Tab index, Button label, etc ). But I am not prefer to store UI state in ReSwift. Especially instant UI (Dialog, Toast, Navigation) need more efforts and times to control their states. I let UI store their own states. I make efforts to be stored pure data and logics in ReSwift's state.

@DivineDominion
Copy link
Contributor

Closing this because the question appears to have been answered 👍

If any of y'all wants to whip up a short article (e.g. Markdown Gist) that we could link to or include in the docs as an example, please feel very invited to do so 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants