3

In version 1 of RNNavigation, I was sending my store to each screen by this way.

Navigation.registerComponent(RouterConstants.SplashScreen, () => SplashScreen, store, Provider)

But in V2 it seems it's not working. enter image description here

EDIT:

index.js:

import configureNavigation from './routers/app_navigation'
import createStore from './reducers'

const store = createStore()
configureNavigation(store, Provider)

class App extends React.Component {
 constructor (props) {
   super(props)
   .
   .
   .
   this.startApp()
 }

 startApp () {
  Navigation.setRoot({
    stack: {
      children: [{
        component: {
          name: RouterConstants.SplashScreen
        }
      }]
    }
  })
 }
}

const app = new App()

app_navigation.js:

import SplashScreen from '../containers/splash_screen_container'
.....
...


const initializeRouter = (store, Provider) => {
    Navigation.registerComponent(RouterConstants.SplashScreen, () => SplashScreen, store, Provider)
    ....
    ..

}
export default initializeRouter
4
  • 1
    Can you add the code for the navigation setup and also the store? Commented Apr 18, 2018 at 12:09
  • Ideally it would be text, not an image, but that's better than nothing. Now, I find a lot of results for terms in that error here on SO. Did you see any of them? If so, do they not resolve your problem too? If not, then as Pritish said, you should post the full code needed to understand the error, as we don't know what those variables are just by looking at that 1 line. Commented Apr 18, 2018 at 12:12
  • @underscore_d I just added some more codes Commented Apr 18, 2018 at 12:28
  • I am getting an error like 'Application has not been registered.' Commented Dec 6, 2018 at 13:15

3 Answers 3

10

Seems like you can't register to the provider the old way.

Therefore as a workaround, you can create a HOC, that wraps the screen to the provider

Definition

import React from "react";
import { Provider } from "react-redux";

...

function reduxStoreWrapper (MyComponent, store) {
    return () => {
        return class StoreWrapper extends React.Component {
            render () {
                return (
                    <Provider store={store}>
                        <MyComponent />
                    </Provider>
                );
            }
        };
    };
}

Usage

Navigation.registerComponent("RouterConstants.SplashScreen", reduxStoreWrapper(SplashScreen, store))
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, is anyone getting this error? - undefined is not a function (evaluating'(0,_reduxStoreWrapper2.default)(_LoginForm2.default, store)')
It would be great if you could post a question for the issue with relevant details so that users will be able to identify the problem clearly. Thanks
What if I have to register 3 screens? Should I create 3 function reduxStoreWrapper... ?
3

Firstly, thanks Pritish for the solution, but building on the above solution, caused an error for me:

undefined is not a function (evaluating'(0,_withReduxStoreWrapper2.default)(_LoginForm2.default, store)'

I modified Navigation.registerComponent() to return a function in the 2nd argument, instead of calling it:

export function registerScreens() {
    const store = createStore(reducers, applyMiddleware(thunk));
    Navigation.registerComponent('LoginForm', () => withReduxStoreWrapper(LoginForm, store));
}

Here is my modified HOC too:

import React, { Component } from 'react';
import { Provider } from 'react-redux';

const withReduxStoreWrapper = (MyComponent, store) =>
    class StoreWrapper extends Component {
        render() {
            return (
                <Provider store={store}>
                    <MyComponent />
                </Provider>
            );
        }
    };

export default withReduxStoreWrapper;

Comments

0

Defining a Redux store wrapper HOC

A slightly more compact way using an arrow function:

// withReduxStoreWrapper.js
import React from 'react';
import { Provider } from 'react-redux';

const withReduxStoreWrapper = (ReduxScreen, reduxStore) => () => (props) => (
  <Provider store={reduxStore}>
    <ReduxScreen {...props} />
  </Provider>
);

export default withReduxStoreWrapper;

Registering the screens

You can then register the screen as follow:

// index.js
// ...

const registerScreens = (store) => {
  Navigation.registerComponent('MyReduxScreen', withReduxStoreWrapper(MyReduxScreen, store));
  Navigation.registerComponent('MyNormalScreen', () => MyNormalScreen);
}

const store = configureStore();
registerScreens(store);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.