Proper use of react-redux connect
我是react-redux的新手,我在这里阅读文档https://github.com/reactjs/react-redux/blob/master/docs/api.md
该文档说
connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])
但随后我看到此示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import React from 'react' import { connect } from 'react-redux' import { getData } from '../actions' class Content extends React.Component { constructor(props) { super(props); } componentWillMount() { this.props.dispatch(getData()) } render() { const { data } = this.props; //This data is the same returned for fungetStore return ( { data.text } ); } } const fungetStore = store => { return { data: store //Return the content of the store } } Content = connect(fungetStore)(Content) export default Content |
您可以在代码中看到在连接中发送了
在此示例中,
此外,
1 2 3 4 5 | connect(mapState, mapDispatch)(MyComponent) // use state and dispatch actions via functions connect(mapState)(MyComponent) // use state connect(null, mapDispatch)(MyComponent) // dispatch actions via functions connect(null, null)(MyComponent) // dispatch actions via dispatch() connect()(MyComponent) // dispatch actions via dispatch() |