3 回答
TA贡献1815条经验 获得超6个赞
<Text>{bookData}</Text>
bookData 是一个数组。您不能使用内置组件显示数组。做这样的事情:
<View>
<Button title="Get Sample Book" onPress={getSampleBookDataHandler} />
{bookData.map((item) => (
<Text>{item.title}</Text>
))}
</View>
TA贡献1712条经验 获得超3个赞
由于它是一个对象数组,因此您应该遍历该数组,这map是一个选项,也可以添加一个选项key,以便 React 可以更有效地更新组件。所以在 BookComponent 的返回值中你可以有
return (
<View>
<Button title="Get Sample Book" onPress={getSampleBookDataHandler} />
{bookData.map((book) => <Text key={book.title}>{book.title}</Text>)}
</View>
);
TA贡献1946条经验 获得超4个赞
getSampleBookDataHandler 数据应该是相同的对象格式,如 { pages:1, title:'xxyy'}
import React, {useState} from 'react';
import {Text, View, Button} from 'react-native';
import getSampleBookData from './BookPresenter';
const BookComponent = () => {
const [bookData, setBookData] = useState([]);
const getSampleBookDataHandler = () => {
setBookData(getSampleBookData);
};
useState(() => {
getSampleBookDataHandler();
},[getSampleBookDataHandler])
return (
<View>
<Button title="Get Sample Book" onPress={getSampleBookDataHandler} />
{bookData && <Text>{bookData.title}</Text> }
</View>
);
};
export default BookComponent;
添加回答
举报