Antd library: How to approach when trying to render an image?

喜夏-厌秋 提交于 2020-05-17 14:44:18

问题


How to approach when trying to render an image? I am trying below but its not showing image.

const columns = [
  {
    title: 'image',
    dataIndex: 'notificationImage.url',
    key: 'notificationImage.url',
    render: (record) =>
    //console.log(text, record && record.notificationImage && record.notificationImage.url, index)
    {
      record && record.notificationImage &&
        <img
          width="460" height="345"
          src={record.notificationImage.url} />
    }
  },]

回答1:


I think your code block should look like this :

Add return

render: (record) =>
    {
      return record && record.notificationImage && // <---- HERE
        <img
          width="460" height="345"
          src={record.notificationImage.url} />
    }

OR

remove curly braces as you have one line execution

render: (record) => record && record.notificationImage &&
        <img
          width="460" height="345"
          src={record.notificationImage.url} />

I have also added code snippet, I hope that will clear your doubts

const { useState , useEffect } = React;

const App = () => {

  var res = {
    "image" : {
      "url" : "https://i.stack.imgur.com/GLI4g.png?s=328&g=1"
    }
  }

  const getImage = () => {
    res && res.image && <img src={res.image.url} /> 
  }
  
  const getImage2= () => {
    return res && res.image && <img src={res.image.url} /> 
  }
  
  const getImage3= () => res && res.image && <img src={res.image.url} />;

  return (
    <div>
      First :
      {getImage()}
      <hr/>
      Second :
      {getImage2()}
      <hr/>
      Third :
      {getImage3()}
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById('react-root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react-root"></div>


来源:https://stackoverflow.com/questions/61723295/antd-library-how-to-approach-when-trying-to-render-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!