1

如何将 Redux 连接到比父组件低几层的子组件?我不知道如何在store不爆炸的情况下进入道具。

我没有使用 IronRouter,而是使用带有 React 0.13 的 Redux 3。这是一个例子:

const SearchBar = React.createClass({
  render() {
    return <div> {this.props.text} </div>;
  }
});


function mapStateToProps(state) {
  return {text: state.search.text};
}

_SearchBar = connect(mapStateToProps)(SearchBar);

在“连接(搜索栏)”的上下文或道具中找不到“商店”。要么将根组件包装在 a 中<Provider>,要么将“store”作为道具显式传递给“Connect(SearchBar)”。

4

2 回答 2

4

您要么需要通过中间组件一直向下传递商店,要么就像消息中所说的那样,使用<Provider>应用程序顶部的组件,它将自动向下传递。

于 2015-10-05T22:19:28.727 回答
2

1)将您的根组件包装在<Provider store={store}></Provider>

class Root extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <YourAppComponent />
      </Provider>
    )
  }
}

Provider通过 React 的context传递 store 。

2)你connect()现在应该工作:),但前提是你所有连接的组件都是<YourAppComponent />

于 2015-10-06T09:17:30.323 回答