简体   繁体   English

如何使用React.js提取API

[英]How to use React.js Fetching API

I'm learning React and practicing how to use 'fetch' in React. 我正在学习React,并练习如何在React中使用“提取”。 I have successfully fetched some API. 我已经成功获取了一些API。 I listed some random content out of this API, such as 'Title', 'Author' and 'Points'. 我从该API中列出了一些随机内容,例如“标题”,“作者”和“积分”。 However, I noted that some don't have the 'Title'. 但是,我注意到有些没有“标题”。 I don't like those that don't have 'Title'(leaving it blank). 我不喜欢那些没有“标题”(留空)的内容。 I'd like to automatically add 'A/N' in those lists that don't have the 'Title'. 我想在没有“标题”的那些列表中自动添加“ A / N”。

Does anybody can teach me how to do that? 有人可以教我该怎么做吗?

Here is my code: 这是我的代码:

import React, { Component } from 'react';
import './App.css';

class App extends Component{
  constructor(){
  super();
  this.state={
     hits: [],
     isLoading: false,
     error: null,
   }
}

componentDidMount(){
  this.setState({
     isLoading: true
    })
  fetch('https://hn.algolia.com/api/v1/search?query=')
  .then(response => {
       if(response.ok){
       return response.json()
      }else{
      throw new Error('Something went wrong...')
    }
 })
  .then(data => this.setState({
    hits: data.hits,
    isLoading: false
  }))
  .catch(error => this.setState({
     error: null, 
     isLoading: false
  }))
 }

render(){
const {hits, isLoading, error} = this.state;

    if(isLoading){
      return <p>Loading ... </p>
    }
    if(error){
      return <p>{error.message}</p>
     }
return(
    <div className="container">

        {hits.map(data => 
          <ul key={data.objectID}>   
            <li><a href={data.url}>Title: {data.title}</a></li>
            <li>Author:{data.author}</li>
            <li>Points: {data.points}</li>
          </ul> 
        )}
    </div>
   )
  }
}
export default App

You can make use of !! 您可以利用! operator to achieve this Here is you code 操作员实现这一点,这是您的代码

 import React, { Component } from 'react'; import './App.css'; class App extends Component{ constructor(){ super(); this.state={ hits: [], isLoading: false, error: null, } } componentDidMount(){ this.setState({ isLoading: true }) fetch('https://hn.algolia.com/api/v1/search?query=') .then(response => { if(response.ok){ return response.json() }else{ throw new Error('Something went wrong...') } }) .then(data => this.setState({ hits: data.hits, isLoading: false })) .catch(error => this.setState({ error: null, isLoading: false })) } render(){ const {hits, isLoading, error} = this.state; if(isLoading){ return <p>Loading ... </p> } if(error){ return <p>{error.message}</p> } return( <div className="container"> {hits.map(data => <ul key={data.objectID}> <li><a href={data.url}>Title: {!!data.title ? data.title : "A/N" }</a></li> <li>Author:{data.author}</li> <li>Points: {data.points}</li> </ul> )} </div> ) } } export default App 

ALternative way to achieve this Set const default_title to 'n/a'; 实现此目的的另一种方法将const default_title设置为'n / a'; and use in render function. 并用于渲染功能。

You can use map() and save data like this: 您可以使用map()并保存数据,如下所示:

fetch('https://hn.algolia.com/api/v1/search?query=')
  .then(response => {
       if(response.ok){
       return response.json()
      }else{
      throw new Error('Something went wrong...')
    }
 })
  .then(data => this.setState({
    hits: data.hits.map( item => {
      if(!item.Title){
        item.Title = 'A/N';
      }
      return item;
    }),
    isLoading: false
  }))
  .catch(error => this.setState({
     error: null, 
     isLoading: false
  }))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM