react-redux

  • 在入口页面,引入 Provider,将 store 作为 props 传入
import { Provider } from 'react-redux'

const store = createStore(todoApp)

render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
)
  • mapStateToProps 返回 state 作为 props 使用
const mapStateToProps = state => {return{key:state}}
  • mapDispatchToProps 返回  dispatch actions 作为 props 使用
const mapDispatchToProps = dispatch => {
  return {
    key: () => {
      dispatch(action creator)
  }
}
  • 连接 Redux 和 React
import { connect } from 'react-redux'

const ConnectedComponent = connect(mapStateToProps, mapDispatchToProps)(Component)
  • 连接 Redux 和一个 Test App
const Container = connect(mapStateToProps,mapDispatchToProps)(Presentational)

class AppWrapper extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <Provider store={store}>
        <Container />
      </Provider>
    );
  }
};