首页 > 解决方案 > React Native:从方法中获取文本元素的值

问题描述

我正在尝试调用一个方法,通过在方法中返回它来用 React Native 中的 Text 元素填充 View 元素:

<查看>{this._getCategories(item)}</查看>

_getCategories 方法在使用 item 参数检索正确数据后返回一个 Text 元素。简化版本如下所示:

_getCategories = item => {
names = 'some string';
      console.log(names); //this logs the correct string
      return <Text style={styles.categories}>{names}</Text>;  
};

Text 元素不会出现在应用程序中,如果我在 View 元素中使用 {String(names)} 则显示未定义。

标签: react-native

解决方案


两者都试了{String(names)}{names}效果很好。可能是您的检索方法有问题。


推荐阅读