簡體   English   中英

"如何在 ReactJS 中使用 axios 從 json 數據(api)中獲取特定數據"

[英]How do i get specific data from a json data (api) using axios in ReactJS

我想從 Instagram 圖形 api 的標題中獲取帶有特殊文本的圖像。例如,如果圖像的標題在 Instagram 中具有“是”字樣,則必須在主頁中顯示。問題是我知道如果在 axios 中承諾我不能寫,或者我可能不知道如何編寫此規則。謝謝您的幫助。

 import React from 'react' import '../styles/home.css' import axios from 'axios'; export default class Home extends React.Component { state = { posts: [] } componentDidMount() { axios.get(`https://graph.instagram.com/me/media?fields=id,caption,media_url,permalink,username&access_token=IGQ....`) .then(res => { const posts = res.data.data; this.setState({ posts }); }) } render() { return ( <div> { this.state.posts .map(post => <div className="banner_image"> <img className="post_img" key={post.id} src={post.media_url} alt="image"/> <div className="banner_text"> <div className="banner_content"> { this.state.posts .map(post => <h1 className="main_title" key={post.id}>{post.username}</h1> ) } {this.state.posts .map(post => <h3 className="main_caption" key={post.id}>{post.caption}</h3> ) } </div> </div> </div> ) } { this.state.posts .map(post => <img className="post_img_food" key={post.id} src={post.media_url} alt="image"/> ) } </div> ) } }

您可以嘗試 .indexOf 並創建一個 if 語句來查找字符串“yes”,如果它在那里,它將顯示該圖片,如果不是 elif 或 end。

您可以使用 RegExp(正則表達式)或 ES6 內置函數,如 . include (),.search() 檢查是否在標題中找到是,然后使用三元運算符。

"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM