问题
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