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()
,可以自动监听变化 -
mapStateToProps
将store
中state
数据映射成props
对象,展示组件就可以通过this.props.
来访问容器组件返回的数据 -
mapDispatchToProps
将action
作为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);
网友评论