React Native:从方法获取Text元素的值

时间:2018-10-05 18:29:04

标签: 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>;  
};

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

1 个答案:

答案 0 :(得分:1)

尝试{String(names)}{names}都可以。检索方法可能有问题。