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
2.2 HomeView container
import {
Header,
Main,
} from '../components/home-view';
import Actions from '../actions';
class HomeView extends Component {
render() {
return (
<View>
<Header {...this.props}/>
<Main {...this.props} isVisible={this.state.isVisible}/>
</View>
);
}
}
function mapStateToProps(state) {
return {
todos: state.todos
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(Actions, dispatch)
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(HomeView);
原文: https://unbug.gitbooks.io/react-native-training/content/43containers&_components.html