首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理rest api中的图像和链接并在React中显示它们的最佳方法

处理rest api中的图像和链接并在React中显示它们的最佳方法
EN

Stack Overflow用户
提问于 2019-12-19 10:30:27
回答 2查看 32关注 0票数 1

我是第一次使用React,我从一个API源中提取数据,然后在视图中显示这些数据,不像Angular,你可以在源码中使用src={{image.path}},它不能在React中工作,因此,让图像和href urls正常工作和显示的最好方法是什么?

到目前为止我的代码是:

代码语言:javascript
复制
import React, { Component } from 'react';
import '../about/about.css';

class About extends Component {

    state = {
        loading: false,
        data: []
    }

    componentDidMount() {
        this.setState({ loading: true })
        console.log('app mounted');
        fetch('https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=8')
            .then(data => data.json())
            .then(data => this.setState({ data: data.articles, loading: false }))
    }

    render() {
        return (
            <div className="about">
                {this.state.loading
                    ? "loading..."
                    : <div>
                        {this.state.data.map((post, indx) => {
                            return (
                                <div className="container about text-left mt-5" key={indx}>
                                    <h5>{post.title}</h5>
                                    <p>{post.description}</p>

                                    <div className="media">
                                        <img className="align-self-start mr-3" src="{post.urlToImage}" alt="Alt text"></img>
                                        <div className="media-body">
                                            <h5 className="mt-0">Top-aligned media</h5>
                                            <a href="{{post.url}}" target="_blank">Read More</a>
                                        </div>
                                    </div>
                                </div>
                            )
                        })}

                    </div>
                }
            </div>
        )
    }
}
export default About;

src="{post.urlToImage}"不能工作,<a href="{{post.url}}" target="_blank">Read More</a>也不能工作?

处理这个问题的最好方法是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-19 10:33:49

无需使用""传入字符串,只需使用{}传入您要查找的属性即可。

代码语言:javascript
复制
src={post.urlToImage}
href={post.url}
票数 2
EN

Stack Overflow用户

发布于 2019-12-19 10:37:36

在引号中使用表示字符串输出的东西:src="{post.url}",只需删除引号以将动态值作为道具(如src={post.url} )传递

代码语言:javascript
复制
import React, { Component } from 'react';
import '../about/about.css';

class About extends Component {

    state = {
        loading: false,
        data: []
    }

    componentDidMount() {
        this.setState({ loading: true })
        console.log('app mounted');
        fetch('https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=8')
            .then(data => data.json())
            .then(data => this.setState({ data: data.articles, loading: false }))
    }

    render() {
        return (
            <div className="about">
                {this.state.loading
                    ? "loading..."
                    : <div>
                        {this.state.data.map((post, indx) => {
                            return (
                                <div className="container about text-left mt-5" key={indx}>
                                    <h5>{post.title}</h5>
                                    <p>{post.description}</p>

                                    <div className="media">
                                        <img className="align-self-start mr-3" src={post.urlToImage} alt="Alt text"></img>
                                        <div className="media-body">
                                            <h5 className="mt-0">Top-aligned media</h5>
                                            <a href={post.url} target="_blank">Read More</a>
                                        </div>
                                    </div>
                                </div>
                            )
                        })}

                    </div>
                }
            </div>
        )
    }
}
export default About;

快乐的编码

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59402555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档