React Native组件学习之设置动态参数

绝地灬酷狼 2023-07-21 11:10 89阅读 0赞

###NavBar.js

  1. import React, {Component,} from 'react'
  2. import {
  3. StyleSheet,
  4. View,
  5. Animated,
  6. TouchableOpacity,
  7. TouchableNativeFeedback,
  8. Platform
  9. } from 'react-native'
  10. import px2dp from './px2dp'
  11. import Icon from 'react-native-vector-icons/Ionicons'
  12. const PropTypes = require('prop-types');
  13. export default class NavBar extends Component {
  14. static propTypes = { //接收以下六个参数:
  15. title: PropTypes.string, //标题
  16. leftIcon: PropTypes.string, //左边图标
  17. rightIcon: PropTypes.string, //右边图标
  18. leftPress: PropTypes.func, //左边点击事件方法
  19. rightPress: PropTypes.func, //右边点击事件方法
  20. style: PropTypes.object //style样式
  21. }
  22. static topbarHeight = (Platform.OS === 'ios' ? 64 : 42)
  23. renderBtn(pos) {
  24. let render = (obj) => {
  25. const {name, onPress} = obj;
  26. if (Platform.OS === 'android') {
  27. return (
  28. <TouchableNativeFeedback onPress={onPress} style={styles.btn}>
  29. <Icon name={name} size={px2dp(26)} color="#fff"/>
  30. </TouchableNativeFeedback>
  31. )
  32. } else {
  33. return (
  34. <TouchableOpacity onPress={onPress} style={styles.btn}>
  35. <Icon name={name} size={px2dp(26)} color="#fff"/>
  36. </TouchableOpacity>
  37. )
  38. }
  39. }
  40. if (pos == "left") {
  41. if (this.props.leftIcon) {
  42. return render({
  43. name: this.props.leftIcon,
  44. onPress: this.props.leftPress
  45. })
  46. } else {
  47. return (<View style={styles.btn}></View>)
  48. }
  49. } else if (pos == "right") {
  50. if (this.props.rightIcon) {
  51. return render({
  52. name: this.props.rightIcon,
  53. onPress: this.props.rightPress
  54. })
  55. } else {
  56. return (<View style={styles.btn}></View>)
  57. }
  58. }
  59. }
  60. render() {
  61. return (
  62. <View style={[styles.topbar, this.props.style]}>
  63. {this.renderBtn("left")}
  64. <Animated.Text numberOfLines={1}
  65. style={[styles.title, this.props.titleStyle]}>{this.props.title}</Animated.Text>
  66. {this.renderBtn("right")}
  67. </View>
  68. )
  69. }
  70. }
  71. const styles = StyleSheet.create({
  72. topbar: {
  73. height: NavBar.topbarHeight,
  74. backgroundColor: "#1E82D2",
  75. flexDirection: 'row',
  76. justifyContent: 'space-between',
  77. alignItems: 'center',
  78. paddingTop: (Platform.OS === 'ios') ? 20 : 0,
  79. paddingHorizontal: px2dp(10)
  80. },
  81. btn: {
  82. width: 40,
  83. height: 40,
  84. justifyContent: 'center',
  85. alignItems: 'center'
  86. },
  87. title: {
  88. color: "#fff",
  89. fontWeight: "bold",
  90. fontSize: px2dp(16),
  91. marginLeft: px2dp(5),
  92. }
  93. });

####px2dp.js

  1. import {Dimensions} from 'react-native'
  2. const deviceW = Dimensions.get('window').width
  3. const basePx = 375
  4. export default function px2dp(px) {
  5. return px * deviceW / basePx;
  6. }

那么我们在调用NavBar组件时只需要传入title、leftIcon、leftPress、rightIcon和rightPress就可以了:

  1. <NavBar
  2. title="我的"
  3. leftIcon="ios-notifications-outline"
  4. leftPress={this.leftPress.bind(this)}
  5. rightIcon="ios-settings-outline"
  6. rightPress={this.rightPress.bind(this)}/>

在这里插入图片描述

js

  1. <NavBar
  2. title="新闻"
  3. titleStyle={styles.titleStyle}
  4. style={styles.style}/>

css

  1. style: {
  2. height: NavBar.topbarHeight,
  3. backgroundColor: "#fff",
  4. flexDirection: 'row',
  5. justifyContent: 'space-between',
  6. alignItems: 'center',
  7. paddingTop: (Platform.OS === 'ios') ? 20 : 0,
  8. paddingHorizontal: px2dp(10)
  9. },
  10. titleStyle: {
  11. fontSize: 16,
  12. color: '#000',
  13. fontWeight: "bold",
  14. }

注意style的命名一定要跟上面this.props后面的参数一致!

发表评论

表情:
评论列表 (有 0 条评论,89人围观)

还没有评论,来说两句吧...

相关阅读