第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何從 React Native 中的 api 返回 JSON 中的隨機(jī)數(shù)據(jù)

如何從 React Native 中的 api 返回 JSON 中的隨機(jī)數(shù)據(jù)

白板的微信 2023-08-10 11:04:23
我有一個(gè)從 API 獲取數(shù)據(jù)的函數(shù)。我想從數(shù)據(jù)中返回一個(gè)隨機(jī)數(shù)據(jù)。我已經(jīng)盡了一切努力,但似乎沒(méi)有解決辦法。下面是從 API 調(diào)用 JSON 數(shù)據(jù)的函數(shù)。const fetchLastestPost = async () => {        const response = await fetch(            `https://learnersnews.com/wp-json/wp/v2/case?per_page=10&page=${page}`,        );        const post = await response.json();        if (page == 1) {            setPosts(post);                 } else {            setPosts([...posts, ...post]);                   }        setIsFetching(false);        setIsLoading(false)    }展開(kāi)片段下面是 Flatlist 組件 <FlatList                data={posts}                onRefresh={() => onRefresh()}                refreshing={isFetching}                onEndReached={() => handleLoadMore()}                onEndReachedThreshold={0.1}                showsVerticalScrollIndicator={false}                ListFooterComponent={() => renderFooter()}                renderItem={({ item }) => (                                   <TouchableOpacity activeOpacity={0.8} onPress={() =>            navigation.navigate('Singlecase', {                post_id: item.id,                name: item.title.rendered,            })        }>                 <View>                          <Card>            <CardItem header bordered style={{ paddingTop:8, paddingBottom: 8,}}>              <Text  numberOfLines={2}             ellipsizeMode='tail'  style={styles.titleText} >            {item.title.rendered}</Text>            </CardItem>                       <CardItem bordered style={{ paddingTop:1, paddingBottom:1 }}>            
查看完整描述

1 回答

?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

你嘗試過(guò)javascript地板方法嗎?我認(rèn)為如果你給 ids 分配了一個(gè)數(shù)字,它會(huì)幫助你獲得一個(gè)隨機(jī)的 JSON。


你可以寫(xiě):


const fetchLastestPost = async () => {

    let page = Math.floor(100)

    const response = await fetch(

        `https://learnersnews.com/wp-json/wp/v2/case?per_page=10&page=${page}`,

    );

    ...

}

或者,如果 ids 未排序,那么您可以嘗試將 ids 放入數(shù)組中:


const fetchLastestPost = async () => {

        let value = Math.floor(4);

        let page = [1,3,4,7,10]

        const response = await fetch(

            `https://learnersnews.com/wp-json/wp/v2/case?per_page=10&page=${page[value]}`,

        );

        ...

    }

或者,如果您想在不知道 id 和序列的情況下獲取數(shù)據(jù),請(qǐng)嘗試:


const fetchLastestPost = async () => {

let page = Math.floor(100) //add totalCount in number which you can get from server side.

    const response = await fetch(

        `https://learnersnews.com/wp-json/wp/v2/case?per_page=10&page=${page}`,

    );

    if(response == ""){

        fetchLastestPost();

    }

    ...

}


查看完整回答
反對(duì) 回復(fù) 2023-08-10
  • 1 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)