3.4 Inheritance

1.pass styles as props

  1. class InheritanceStyle extends Component {
  2. render() {
  3. return (
  4. <View style={this.props.parentColor}>
  5. </View>
  6. );
  7. }
  8. }
  9. class Main extends Component {
  10. handleReady(str){
  11. console.log(str);
  12. }
  13. render() {
  14. return (
  15. <View style={styles.container}>
  16. <InheritanceStyle parentColor={styles.blue}/>
  17. </View>
  18. );
  19. }
  20. }
  21. const styles = StyleSheet.create({
  22. container: {
  23. flex: 1
  24. },
  25. blue: {
  26. flex: 1,
  27. backgroundColor: 'blue'
  28. }
  29. });

2.concatenation styles

BaseStyles.js

  1. import { StyleSheet,Dimensions } from 'react-native';
  2. let winSize = Dimensions.get('window');
  3. const BaseStyles = StyleSheet.create({
  4. text: {
  5. fontSize: 40/winSize.scale
  6. }
  7. });
  8. export default BaseStyles;
  1. import BaseStyles from './BaseStyles';
  2. class InheritanceStyle extends Component {
  3. render() {
  4. return (
  5. <View style={this.props.parentColor}>
  6. <Text style={[BaseStyles.text, styles.text]}> this is a long text </Text>
  7. </View>
  8. );
  9. }
  10. }
  11. const styles = StyleSheet.create({
  12. text:{
  13. color: '#ffffff'
  14. }
  15. });

 3.4 Inheritance  - 图1

原文: https://unbug.gitbooks.io/react-native-training/content/34_inheritance.html