React API参考

时间:2018-11-02 04:02:35

标签: reactjs axios

enter image description here

“列表”是“ Axios.get”中的API_Call。

我想检查API“ src”是否为空。

我该如何检查。

4 个答案:

答案 0 :(得分:1)

也许您可以使用像这样的条件逻辑来渲染result.volumeInfo.imageLinks.thumbnail的图像,或者在result.volumeInfo.imageLinks.thumbnail未定义的情况下渲染其他后备图像?

所以,如下所示:

<Card key={result.id}
  hoverable
  style={{ width:240 }}
  cover={ () => {

    const cardImage = result.volumeInfo.imageLinks.thumbnail

    // If result.volumeInfo.imageLinks.thumbnail is defined, render
    // image with it, otherwise rendering some other fallback image
    return (cardImage ? 
      <img alt="example" src={ cardImage } /> :
      <img alt="example" src="/some-empty-image.jpg" />)
  }} >    

  <Meta title={result.volumeInfo.title} />
</Card>

答案 1 :(得分:0)

Call a get API using axios and check response.
// Make a request for a list
 axios.get('URL path')
  .then(function (response) {
    console.log(response);
    //put ur condition code here if exits or not whatever u want
  })
  .catch(function (error) {
    console.log(error);
  });

答案 2 :(得分:0)

这应该有效

src={result.volumeInfo.title == "" ? "empty-src" : result.volumeInfo.title }

答案 3 :(得分:0)

<Card key={result.id}
  hoverable
  style={{ width:240 }}
  cover={ () => {

    const cardImage = result.volumeInfo.imageLinks.thumbnail

    // If result.volumeInfo.imageLinks.thumbnail is defined, render
    // image with it, otherwise rendering some other fallback image
    return (cardImage ? 
      <img alt="example" src={ cardImage } /> :
      <img alt="example" src="/some-empty-image.jpg" />)
  }} >    

  <Meta title={result.volumeInfo.title} />
</Card>