0

我使用带有 redux 的 WiX 导航。 https://wix.github.io/react-native-navigation/#/usage

在 app.jsx 文件中,我将所有屏幕都注册到 redux 商店:

const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const reducer = combineReducers(reducers);
const store = createStoreWithMiddleware(reducer);
registerScreens(store, Provider);

在 registerSceens.js 我绑定了 redux 商店:

export default (store, Provider) =>  {
Navigation.registerComponent('aApp.FooComponent', () => FooComponent, store, 
Provider);
}

那么在 FooComponent 中,我怎样才能轻松访问 redux 存储呢?

export default class FooComponent extends Component {
constructor(props) {
    super(props);
    console.log(store); //i need REDUX store 
}
4

2 回答 2

1

您需要连接您的组件,然后您可以从上下文中获取商店,如下所示:

class FooComponent extends Component {
  static contextTypes = {
    store: object,  // <--- allow this component to access the store
  };

  componentWillMount(props) {
     const { store } = this.context; // <-- Get it!!
     console.log(store); //i need REDUX store 
  }
}
export default connect()(FooComponent);

定义 后contextTypes,您可以从 访问商店this.context

虽然你可以做到这一点......问题是......你为什么要这样做?connect已经允许您访问状态和绑定操作,这通常是您所需要的。根据经验,让我们的组件尽可能地哑总是一个好主意。

于 2018-01-11T18:42:23.220 回答
0

您需要FooComponent使用 react-redux 将您的连接到 redux。之后,您可以通过道具访问任何内容。

于 2018-01-11T11:28:01.190 回答