美文网首页
react-redux-Provider/connect

react-redux-Provider/connect

作者: 07120665a058 | 来源:发表于2017-05-22 20:16 被阅读39次

Provider

  • 只有一个子组件,没有或者超过一个均会报错
  • store属性,可以得到redux里所有的state
  • Provider里所有的子组件都可以通过this.props得到state
import React from 'react'
import ReactDom from 'react-dom';
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import reducers from './reducers'
import App from './containers/App'
let store = createStore(reducers)
ReactDom.render(
      <Provider store={store}>
        <App />
      </Provider>,
      document.getElementById('root'));

connect

  • connect里有subscribe(),可以自动监听变化
  • mapStateToPropsstorestate数据映射成props对象,展示组件就可以通过this.props.来访问容器组件返回的数据
  • mapDispatchToPropsaction作为props绑定到组件上
import {connect} from 'react-redux';
import App from '../components/App';
const mapStateToProps = (state)=> {
    return{
       count: state.count
      }
};
const mapDispatchToProps = (dispatch)=> {
    return {
        onAdd: (content)=> {
            dispatch({type:'ADD',content});
        }
    }
};
export default connect(mapStateToProps, mapDispatchToProps)(App);

相关文章

网友评论

      本文标题:react-redux-Provider/connect

      本文链接:https://www.haomeiwen.com/subject/odyixxtx.html