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

Explore using NavigationTabsReducer, NavigationStackReducer, .etc #7

Closed
respectTheCode opened this issue Mar 23, 2016 · 5 comments
Closed

Comments

@respectTheCode
Copy link

RN includes several reducers that look like they should work with redux. Has any explored this?

https://github.com/facebook/react-native/tree/master/Libraries/NavigationExperimental/Reducer

@bnjmnt4n
Copy link

Implementing our own reducers, which doesn't cover all the functionality of the navigation, seems like a pain long-term. Although the format might be different, perhaps we could base it off the included reducers?

@respectTheCode
Copy link
Author

I did some experimenting this weekend and TabsReducer and StackReducer work with redux.

const combineReducers = require("redux").combineReducers;

const React = require("react-native");
const {
    NavigationExperimental
} = React;
const {
    Reducer: NavigationReducer
} = NavigationExperimental;

let counter = 0;

module.exports = combineReducers({
    nav: NavigationReducer.TabsReducer({
        key: "tabExample",
        initialIndex: 0,
        tabReducers: [
            NavigationReducer.StackReducer({
                getPushedReducerForAction: action => {
                    if (action.tab === 0) {
                        return state => (state || {key: "tab-0-page-" + (counter++), type: action.type});
                    }
                    return null;
                },
                initialState: {
                    key: "tab-0-page",
                    label: "Tab 0",
                    index: 0,
                    children: [
                        {key: "Tab 0 Root", type: "page"}
                    ]
                }
            }),
            NavigationReducer.StackReducer({
                getPushedReducerForAction: action => {
                    if (action.tab === 1) {
                        return state => (state || {key: "tab-1-page-" + (counter++), type: action.type});
                    }
                    return null;
                },
                initialState: {
                    key: "tab-1-page",
                    label: "Tab 1",
                    index: 0,
                    children: [
                        {key: "Tab 1 Root", type: "page"}
                    ]
                }
            })
        ]
    })
});

@jlyman
Copy link
Owner

jlyman commented Mar 30, 2016

Good example @respectTheCode. I think it can go both ways. I'm using a hybrid of the two right now, where the dev would control the actions and basic reducers, but where the reducers are hooking into NavigationStateUtils to do some of the state manipulation. Doing so gives a little more control over what happens during these state transitions, more opportunities to hook into it.

I wrote the examples this way on purpose to make it more accessible to those of us familiar to seeing Redux style reducers, but this is very valuable as well. I think I'll add a note in the README pointing to your code excerpt as an example of how to do it in a more pure manner.

jlyman added a commit that referenced this issue Mar 30, 2016
…n. Working around absence of any handling of NavigationStateUtils.reset() in NavigationStackReducer.

Partial implementation to go along with #7.
@jlyman
Copy link
Owner

jlyman commented Mar 30, 2016

Put up a branch (pure-rn-reducers) that follows this convention as well for others to see how it might apply in the sample app.

@s4kh
Copy link

s4kh commented Apr 21, 2016

Hi all,

Thanks for the example, I think this is the pretty clean and basic example implementing NavigationExperimental. Even going through the docs in navigation-rfc repo, I am still struggling to get my head around the new navigation concepts. If you guys don't mind, can you please explain me the concepts by answering this stackoverflow question?, so others can also benefit from it.

@jlyman jlyman closed this as completed Apr 22, 2016
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

4 participants