4.3 Containers & Components

1.Presentational and Container Components

Presentational Components Container Components
Purpose
——-
How things look (markup, styles) How things work (data fetching, state updates)
Aware of Redux
——-
No Yes
To read data
——-
Read data from props Subscribe to Redux state
To change data
——-
Invoke callbacks from props Dispatch Redux actions
Are written
——-
By hand Usually generated by React Redux

2.components/home-view & containers/HomeView

2.1 home-view components

 4.3 Containers & Components  - 图1

2.2 HomeView container

  1. import {
  2. Header,
  3. Main,
  4. } from '../components/home-view';
  5. import Actions from '../actions';
  6. class HomeView extends Component {
  7. render() {
  8. return (
  9. <View>
  10. <Header {...this.props}/>
  11. <Main {...this.props} isVisible={this.state.isVisible}/>
  12. </View>
  13. );
  14. }
  15. }
  16. function mapStateToProps(state) {
  17. return {
  18. todos: state.todos
  19. };
  20. }
  21. function mapDispatchToProps(dispatch) {
  22. return {
  23. actions: bindActionCreators(Actions, dispatch)
  24. }
  25. }
  26. export default connect(
  27. mapStateToProps,
  28. mapDispatchToProps
  29. )(HomeView);

原文: https://unbug.gitbooks.io/react-native-training/content/43containers&_components.html