【发布时间】:2018-02-22 05:46:18
【问题描述】:
我试图让一些特定数据出现在一些标签中,但它没有显示。但是,当我 console.log 我试图在 Text 标签中获取的相同数据时,我可以看到它。
这是我的代码
import React, { Component } from 'react';
import {
View,
FlatList,
Text
} from 'react-native'
import { NavigationActions } from 'react-navigation';
import { Header } from '../common';
import Config from '../Config';
export default class Costco extends Component {
constructor() {
super();
this.state = {
stores: [],
};
}
componentWillMount() {
const obj = Config.costcoThree;
const costcoArr = Object.values(obj);
this.setState({
stores: costcoArr,
})
}
renderStores(item) {
const navigate = this.props.navigation;
console.log(item.branchName);
return (
<Text>{item.branchName}</Text>
);
}
render(){
return(
<View>
<Header headerText="Costco"/>
<Text>Hello</Text>
<FlatList
data={this.state.stores}
renderItem={({item}) => {
this.renderStores(item)
//return<Text>{item.branchName}</Text>
}}
keyExtractor={(item) => item.id}
/>
</View>
)
}
}
我尝试了两种不同的方式渲染文本。第一个是通过在 renderItem 方法中返回 Text 标签之间的数据。这非常有效。但是,当我尝试返回函数 renderStores 时,我看不到文本出现。但是当我 console.log 时,数据确实出现了。
【问题讨论】:
标签: react-native react-native-flatlist