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

Navigating when changing redux state #2374

Closed
geniuscd opened this issue Sep 12, 2017 · 11 comments
Closed

Navigating when changing redux state #2374

geniuscd opened this issue Sep 12, 2017 · 11 comments

Comments

@geniuscd
Copy link

geniuscd commented Sep 12, 2017

I'm testing both react navigation (till now I found one drawback only on Modals screens where a workaround must implemented) and react native router flux v4 beta 22. (using redux for both)

I like the idea that in react navigation we can navigate to a screen from a reducer when changing the state of the route, it will simply navigate to another screen.
in RNRF, at least in the examples that i saw, we must call the ACTIONS from within the component (view) in order to navigate. for simple apps thats ok. But for larger apps its advisable that the state handles the screen navigation.

any thought?

discussion

@aksonov
Copy link
Owner

aksonov commented Sep 13, 2017

Duplicate of #2115

@aksonov aksonov marked this as a duplicate of #2115 Sep 13, 2017
@aksonov aksonov closed this as completed Sep 13, 2017
@geniuscd
Copy link
Author

Thank you for your reply. I already read #2115 and didn't understand the fact that i need to call ACTION.... in the reducer? for now it's working but is it the correct way to do it?

case 'user_login_success': Actions.profile(); return state;

@aksonov
Copy link
Owner

aksonov commented Sep 13, 2017

It is possible but it is not fully 'redux' style. So with v4 you could do the same as with react-navigation - integrate your reducer with navigation reducer and pass own dispatch/state into RNRF.

@aksonov
Copy link
Owner

aksonov commented Sep 13, 2017

You should refer to mentioned demo.

@geniuscd
Copy link
Author

@aksonov I tried to implement from this demo link but i was getting an error in the getStateForAction. I guess because it was returning defaultReducer(state, action); instead of return { ...state, PARAMS }
I will try to integrate what you said above and see what will happen.

in the reducer case ActionConst.FOCUS: is only called if and only if Action.PARAMS is called from a view.

@ssomnoremac
Copy link

@geniuscd , yeah I got the same. Did you get around the error?

@geniuscd
Copy link
Author

@ssomnoremac yes :P i used react-navigation

@jordenchang55
Copy link

I also got the error and fixed it.

// it is important to load reducers AFTER actions.create (so no import here)
const reducers = require('./reducers').default;

You should make sure your implementation import the reducer by correct order. When I changed the above line of code to the correct place, and it worked.

@ssomnoremac
Copy link

@jordenchang55 that got me around the error, thanks. My App.js was not configured to the example; however, using the exact same route-reducer and the following, none of the router actions (FOCUS, PUSH, etc) are seen using master RNRF:


const navigator = Actions.create(
    <Stack ...
)
const reducers = require('./reducers').default;

const ReduxReducer = connect((state) => ({ state: state.route }))(Router);

const middleware = [thunk];
const store = compose(
    applyMiddleware(...middleware)
)(createStore)(reducers);

...
                <Provider store={store}>
                    <View style={{flex:1, backgroundColor: "#ccc"}}>
                        <ReduxReducer navigator={navigator} />
                    </View>
                </Provider>

@jordenchang55
Copy link

@ssomnoremac I compared our code, and found that changing your store resolves the problem.

// Your code
const store = compose(applyMiddleware(...middlewares))(createStore)(reducers);
// My code
const store = createStore(
  reducers,
  compose(
    applyMiddleware(...middlewares)
  )
);

@ssomnoremac
Copy link

ssomnoremac commented Sep 16, 2017

hmm, strange, that didn't fix it for me. I'm seeing all actions except the router. It must mean I have something else setup differently. I'm going to have a closer look. I won't give up on this! Thanks for your help @jordenchang55

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