如何将redux状态传递给子路由?

时间:2015-10-20 11:55:19

标签: javascript reactjs react-router redux

我很难理解如何将redux与react-router一起使用。

index.js

[...]

// Map Redux state to component props
function mapStateToProps(state)  {
  return {
    cards: state.cards
  };
}

// Connected Component:
let ReduxApp = connect(mapStateToProps)(App);

const routes = <Route component={ReduxApp}>
  <Route path="/" component={Start}></Route>
  <Route path="/show" component={Show}></Route>
</Route>;

ReactDOM.render(
  <Provider store={store}>
    <Router>{routes}</Router>
  </Provider>,
  document.getElementById('root')
);

App.js

import React, { Component } from 'react';

export default class App extends React.Component {
  render() {
    const { children } = this.props;
    return (
      <div>
      Wrapper
        {children}
      </div>
    );
  }
}

Show.js

import React, { Component } from 'react';

export default class Show extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <ul>
        {this.props.cards.map(card => 
          <li>{card}</li>
        )}
      </ul>
    );
  }
}

抛出

  

未捕获的TypeError:无法读取属性&#39; map&#39;未定义的

我找到的唯一解决方案是使用此代替{children}:

{this.props.children &&
 React.cloneElement(this.props.children, { ...this.props })}

这真的是正确的方法吗?

2 个答案:

答案 0 :(得分:3)

使用react-redux

为了将任何状态或操作创建者注入React组件的props,您可以使用react-redux中的connect,这是Redux的官方React绑定。

值得查看connect here的文档。

作为基于问题中指定内容的示例,您可以执行以下操作:

import React, { Component } from 'react';
// import required function from react-redux
import { connect } from 'react-redux';

// do not export this class yet
class Show extends React.Component {
  // no need to define constructor as it does nothing different from super class

  render() {
    return (
      <ul>
        {this.props.cards.map(card => 
          <li>{card}</li>
        )}
      </ul>
    );
  }
}

// export connect-ed Show Component and inject state.cards into its props.
export default connect(state => ({ cards: state.cards }))(Show);

为了实现这一点,您必须使用Provider中的react-redux包装您的根组件或路由器(这已在您的示例中提供)。但为了清楚起见:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route } from 'react-router';

import { createStore } from 'redux';
import { Provider } from 'react-redux';

import reducers from './some/path/to/reducers';

const store = createStore(reducers);

const routes = <Route component={ReduxApp}>
  <Route path="/" component={Start}></Route>
  <Route path="/show" component={Show}></Route>
</Route>;

ReactDOM.render(
  // Either wrap your routing, or your root component with the Provider so that calls to connect have access to your application's state
  <Provider store={store}>
    <Router>{routes}</Router>
  </Provider>,
  document.getElementById('root')
);

如果任何组件不需要注入任何状态或动作创建者,那么您只需导出一个“哑”React组件,并且在渲染时,您的应用程序状态都不会暴露给该组件。

答案 1 :(得分:0)

我通过在每个组件中使用connect显式映射状态来解决它:

export default connect(function selector(state) {
  return {
    cards: state.cards
  };
})(Show);

通过这种方式,我可以决定组件应该访问的状态的哪些属性,更少地污染道具。不确定这是否是最佳做法。