简体   繁体   中英

React-Native - Redux - access to store

I got a big problem for my project and cannot continue without that.

I have 2 screens : for example one for main page and another for second page. So, there are 2 sections in my Navigation Drawer.

In main page, I save state into my store and get this:

object: {
    fruits: ['apple', 'orange', 'banana'],
    drinks: ['mojito', 'colar']
}

I want to access to the store of mainPage so in my second page I did :

store.getState().

And I got

object: {
    fruits: [],
    drinks: []
}

So I got the same state but it's empty. I don't understand why. If you have any ideas. I can maybe later show you more my code if necessary.

My store :

import { createStore, applyMiddleware } from 'redux';
import logger from 'redux-logger';
import thunk from 'redux-thunk';
import { composeWithDevTools } from 'remote-redux-devtools';
import reducer from '../reducers';

const middleware = applyMiddleware(thunk, logger);

export default function configureStore(initialState) {
  const store = createStore(
    reducer,
    initialState,
    middleware
  );

  return store;
};

For each component, there are a container? Or only one container for all components? I don't really understand that. Sorry I'm new to React-Native, Redux and navigation drawer. It's hard for me to make them works all together..

Maybe the problem is in connect() but I connected them together normally

Sorry it's a little late and hard for me to say anything without seeing your reducers. Within your mainPage, you need to dispatch actions which will trigger a concat to the fruits and drinks array. You can also move your store declaration outside the function declaration and export it. Importing and calling it in other screens will allow you to have access to it, but this isn't the recommended way. The standard way is to call mapStateToProps method and connect that to your component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM