Makes the Vuex store available to the connect() calls in the component hierarchy below. Normally, you can’t use connect() without wrapping a parent or ancestor component in <Provider>.
store(Vuex Store): The single Vuex store in your application.children(ReactElement) The root of your component hierarchy.
ReactDOM.render(
<Provider store={store}>
<MyRootComponent />
</Provider>,
rootEl
)ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Route path="/" component={App}>
<Route path="foo" component={Foo}/>
<Route path="bar" component={Bar}/>
</Route>
</Router>
</Provider>,
document.getElementById('root')
)Connects a React component to a Vuex store.
It does not modify the component class passed to it; instead, it returns a new, connected component class for you to use.
-
[
mapStateToProps(state, [ownProps]): stateProps] (Function): If this argument is specified, the new component will subscribe to Vuex store updates. This means that any time the store is updated,mapStateToPropswill be called. The results ofmapStateToPropsmust be a plain object, which will be merged into the component’s props. If you don't want to subscribe to store updates, passnullorundefinedin place ofmapStateToProps.If your
mapStateToPropsfunction is declared as taking two parameters, it will be called with the store state as the first parameter and the props passed to the connected component as the second parameter, and will also be re-invoked whenever the connected component receives new props as determined by shallow equality comparisons. (The second parameter is normally referred to asownPropsby convention.)The
mapStateToPropsfunction's first argument is the entire Vuex store’s state and it returns an object to be passed as props. -
[
mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Function): If an object is passed, each function inside it is assumed to be a Vuex action creator. An object with the same function names, but with every action creator wrapped into adispatchcall so they may be invoked directly, will be merged into the component’s props.It will be given
dispatchas the first parameter. It’s up to you to return an object that somehow usesdispatchto bind action creators in your own way.If your
mapDispatchToPropsfunction is declared as taking two parameters, it will be called withdispatchas the first parameter and the props passed to the connected component as the second parameter, and will be re-invoked whenever the connected component receives new props. (The second parameter is normally referred to asownPropsby convention.)If you do not supply your own
mapDispatchToPropsfunction full of action creators, the defaultmapDispatchToPropsimplementation just injectsdispatchinto your component’s props. -
[
mapCommitToProps(commit, [ownProps]): commitProps] (Function): If an object is passed, each function inside it is assumed to be a Vuex mutation creator. An object with the same function names, but with every mutation creator wrapped into acommitcall so they may be invoked directly, will be merged into the component’s props.It will be given
commitas the first parameter. It’s up to you to return an object that somehow usescommitto bind mutation creators in your own way.If your
mapCommitToPropsfunction is declared as taking two parameters, it will be called withcommitas the first parameter and the props passed to the connected component as the second parameter, and will be re-invoked whenever the connected component receives new props. (The second parameter is normally referred to asownPropsby convention.)If you do not supply your own
mapCommitToPropsfunction full of mutation creators, the defaultmapCommitToPropsimplementation just injectscommitinto your component’s props. -
[
mapGetterToProps(getters, [ownProps]): stateProps] (Function): If this argument is specified, the new component will subscribe to Vuex store getter updates. This means that any time the store getter is updated,mapGetterToPropswill be called. The results ofmapGetterToPropsmust be a plain object, which will be merged into the component’s props. If you don't want to subscribe to store getter updates, passnullorundefinedin place ofmapGetterToProps.If your
mapGetterToPropsfunction is declared as taking two parameters, it will be called with the store getters as the first parameter and the props passed to the connected component as the second parameter, and will also be re-invoked whenever the connected component receives new props as determined by shallow equality comparisons. (The second parameter is normally referred to asownPropsby convention.)The
mapGetterToPropsfunction's first argument is the entire Vuex store’s state and it returns an object to be passed as props.
A higher-order React component class that passes state and action creators into your component derived from the supplied arguments. This is created by connectAdvanced, and details of this higher-order component are covered there.
function mapStateToProps(state) {
return { todos: state.todos }
}
export default connect(mapStateToProps)(TodoApp)import { addTodo } from './actionCreators'
function mapStateToProps(state) {
return { todos: state.todos }
}
function mapDispatchToProps(dispatch) {
onAddTodo: (id) => dispatch(addTodo(id))
}
export default connect(mapStateToProps, mapDispatchToProps)(TodoApp)function mapStateToProps(state, ownProps) {
return { todos: state.todos[ownProps.userId] }
}
export default connect(mapStateToProps)(TodoApp)-
Since
connectreturns a higher-order component, it needs to be invoked two times. The first time with its arguments as described above, and a second time, with the component:connect(selectorFactory)(MyComponent). -
connectdoes not modify the passed React component. It returns a new, connected component, that you should use instead.