React Native:在函数内部访问 this.props

杰夫·吉尔伯特

我设法使用以下代码获取数据并显示给 UI:

export default class BoxGarage extends Component {
    render() {
        let garage = this.props.garage;
        garage.name = garage.name.replace('strtoreplace', 'My Garage');

        let cars = garage.cars.length ?
            garage.cars.map((val, key) => {
                return (
                    <Car key={key} car={val} />
                )
            }) : (
                <View style={styles.boxEmpty}>
                    <Text style={styles.textEmpty}>(No Cars)</Text>
                </View>
            );

        return (
            <View style={styles.boxGarage}>
                <Text>{ garage.name }</Text>
                { cars }
            </View>
        )
    }       
}

然后我试图用一个函数来改变,但没有显示汽车。什么东西少了?

export default class BoxGarage extends Component {
    render() {
        let garage = this.props.garage;
        garage.name = garage.name.replace('strtoreplace', 'My Garage');

        cars = function(garage) {
            if (garage.cars.length) {
                garage.cars.map((val, key) => {
                    return (
                        <Car key={key} car={val} />
                    );
                });
            }
            else {
                return (
                    <View style={styles.boxEmpty}>
                        <Text style={styles.textEmpty}>(No Cars)</Text>
                    </View>
                );
            }
        }

        return (
            <View style={styles.boxGarage}>
                <Text>{ garage.name }</Text>
                { cars(this.props.garage) }
            </View>
        )
    }       
}

而且我认为我应该使用构造函数重构最佳实践,或者只是将函数移到渲染之外,但我不知道它是什么。请指教。

尼古拉斯大厦

您的第二个代码不起作用的原因是,如果garage.cars.length > 0,您不会从函数中返回任何内容。

if (garage.cars.length) {
  // Added a return statement on the next line
  return garage.cars.map((val, key) => {
    return (
      <Car key={key} car={val} />
    );
  });
}

也就是说,我认为您的代码的第一个版本要清晰得多。如果一段代码变得足够复杂,以至于我很想制作一个内联函数来进行计算,我要么将其拉出到另一个类方法,或另一个组件。不过,在您的情况下,只执行三元或 if/else 会好得多。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

React JS:通过props传递我的处理函数的问题

来自分类Dev

无法在React Native中分配为#<Object>的只读属性'props'

来自分类Dev

React:如何访问this.props.children中的引用

来自分类Dev

React Native从内部函数访问this.prop

来自分类Dev

React:是否可以访问DOM元素的props?

来自分类Dev

ES 6 React类在getter中访问props

来自分类Dev

在React中的props对象上访问键值对中的键名

来自分类Dev

react-native-Picker-未定义不是对象(评估'this.props.children [position] .props)

来自分类Dev

React Native在静态NavigationOptions中使用this.props

来自分类Dev

无法从React组件中的props访问历史记录?

来自分类Dev

在React Native中通过props从数组设置文本

来自分类Dev

React Native props.navigation.navigate不是对象

来自分类Dev

内部函数是否需要参数来访问React功能组件的props和变量

来自分类Dev

将props传递给react函数

来自分类Dev

React Redux-props.createMovie不是函数

来自分类Dev

我的React函数作为props被传递时没有被调用

来自分类Dev

在React功能组件中的函数内部调用props.myFunction()

来自分类Dev

React:是否可以访问DOM元素的props?

来自分类Dev

根据react-native中的props值动态加载图像?

来自分类Dev

为什么我无法在 react native Alert 中访问 this.props.navigation?

来自分类Dev

React Native:创建 props 和 state

来自分类Dev

React 子组件 props : _this2.props.delete 不是函数

来自分类Dev

将 props 的函数传递给 react 中的 render 函数

来自分类Dev

React Native:样式 props.children

来自分类Dev

React Native StyleSheet.create pass props?

来自分类Dev

Ant Design Dropdown - 访问子菜单中的 props 值 [React]

来自分类Dev

什么时候在 React Native 中用花括号包围 props?

来自分类Dev

如何在 React-Native 中将 props 传递给 stackNavigator?

来自分类Dev

未定义(评估'this.props.navigation.navigate')React Native

Related 相关文章

  1. 1

    React JS:通过props传递我的处理函数的问题

  2. 2

    无法在React Native中分配为#<Object>的只读属性'props'

  3. 3

    React:如何访问this.props.children中的引用

  4. 4

    React Native从内部函数访问this.prop

  5. 5

    React:是否可以访问DOM元素的props?

  6. 6

    ES 6 React类在getter中访问props

  7. 7

    在React中的props对象上访问键值对中的键名

  8. 8

    react-native-Picker-未定义不是对象(评估'this.props.children [position] .props)

  9. 9

    React Native在静态NavigationOptions中使用this.props

  10. 10

    无法从React组件中的props访问历史记录?

  11. 11

    在React Native中通过props从数组设置文本

  12. 12

    React Native props.navigation.navigate不是对象

  13. 13

    内部函数是否需要参数来访问React功能组件的props和变量

  14. 14

    将props传递给react函数

  15. 15

    React Redux-props.createMovie不是函数

  16. 16

    我的React函数作为props被传递时没有被调用

  17. 17

    在React功能组件中的函数内部调用props.myFunction()

  18. 18

    React:是否可以访问DOM元素的props?

  19. 19

    根据react-native中的props值动态加载图像?

  20. 20

    为什么我无法在 react native Alert 中访问 this.props.navigation?

  21. 21

    React Native:创建 props 和 state

  22. 22

    React 子组件 props : _this2.props.delete 不是函数

  23. 23

    将 props 的函数传递给 react 中的 render 函数

  24. 24

    React Native:样式 props.children

  25. 25

    React Native StyleSheet.create pass props?

  26. 26

    Ant Design Dropdown - 访问子菜单中的 props 值 [React]

  27. 27

    什么时候在 React Native 中用花括号包围 props?

  28. 28

    如何在 React-Native 中将 props 传递给 stackNavigator?

  29. 29

    未定义(评估'this.props.navigation.navigate')React Native

热门标签

归档