简体   繁体   English

jsx中的img src属性url

[英]img src attribute url in jsx

I am trying to display images from an array, but when I am passing the src value as an expression {} it is just returning in the form of the object instead of a string.can anyone explain? 我试图从数组中显示图像,但是当我将src值作为表达式{}传递时,它只是以对象的形式返回而不是字符串。任何人都可以解释?

export default class Home extends Component{
    constructor(){
        super();
        this.state = {};
    }
    componentWillMount(){
        var url = "https://api.github.com/users";
        request.get(url).then((response) => {
            this.setState({
                users: response.body
            });
        });
    }
    render(){ 
        // this.state.users returns all users information (name, image...)
        const userName = _.map(this.state.users, (user) =>{
            return <li>{user.login}</li>
        });
        const repoURL = _.map(this.state.users, (repoUrl) =>{
            return <li>{repoUrl.repos_url}</li>
        });

        const eachAvatar = _.map(this.state.users, (avatar) =>{
            return <li>{avatar.avatar_url}</li>
        });

        return(
        <div>
        <h2>Here is the list of users in gitHub</h2>
        <table>
        <tr>
        <th>UserName</th>
        <th>Repo URL</th>
        </tr>
        <tr>
        <td>{userName}</td>
        <td>{repoURL}</td>
        <td><img  src={eachAvatar} /></td>
        </tr>
        </table>
        </div>
    );
    }
}

output: 输出:

I'm guessing you want a list of images so render the img tag inside your map: 我猜你想要一个图像列表,所以在你的地图中渲染img标签:

const allAvatars = _.map(this.state.users, (avatar) =>{
            return <li><img src={avatar.avatar_url} /></li>
        });

Then render all avatars 然后渲染所有头像

<td>{allAvatars}</td>

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

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