【问题标题】:Fetching and rendering data from Reddit/REST API [closed]从 Reddit/REST API 获取和呈现数据 [关闭]
【发布时间】:2019-06-15 23:28:38
【问题描述】:

免责声明:我是一名试图学习更多 JavaScript 的设计师。对我放轻松。

我正在创建一个站点,该站点从 reddit api 获取和呈现帖子,并以 CSS 网格/报纸样式布局显示数据。类似于 The Verge。到目前为止,这是我的代码:

const url = "https://www.reddit.com/r/upliftingnews.json?raw_json=1&limit=10"

fetch(url)
    .then(res => res.json())
    .then(res => res.data.children)
    .then(res => res.map(post => ({
        author: post.data.author,
        link: post.data.url,
        img: (typeof (post.data.preview) !== 'undefined') ? post.data.preview.images[0].source.url : null,
        // img: post.data.thumbnail,
        title: post.data.title
    })))
    .then(res => res.map(render))
    .then(res => console.log(res))

// fetch(url)
//     .then(response => response.json())
//     .then(response => {
//         console.log(response.data.children[1].data.preview.images[0].resolutions[1].url);
//     });


const app = document.querySelector('#app');

const render = post => {
    //console.log(post.data);
    const node = document.createElement('div');
    node.innerHTML = `
    <h2>
      <a href="${post.link}">
        <img src="${post.img}" />
        ${post.title}
      </a>
    </h2>`;
    app.appendChild(node);
}

所以这很好用,但是我应该这样做吗?我的问题是:

  1. 我对单个渲染帖子的标记没有任何控制权。我可以使用 CSS 和 nth-child 来单独设置帖子的样式,但这似乎效率低下。
  2. 它的性能似乎不是很好。 html 在页面加载后 1 或 2 秒内呈现,导致空白屏幕闪烁。

谷歌搜索与 fetch api's/rendering json 相关的任何内容都会显示与 react/vue/angular/insertJSLibraryHere 相关的结果。

我真的应该使用这些框架之一吗?如果我刚刚学会了如何使用 React 会更容易/更有效吗?我一直在避免学习 React/Vue,因为我发现自己很难理解,但也许这是一个很好的起点?

当每个搜索结果都与某个 JS 框架相关时,很难找到答案。因此,我为什么在这里问。

【问题讨论】:

    标签: javascript api ecmascript-6 fetch-api reddit


    【解决方案1】:

    React 中的基本获取示例。

    最好将 api 调用的响应保存在 componentWillMount 生命周期方法的状态数组中。我也想使用axios 而不是 fetch。

    这是一个例子:

    import axios from 'axios';
    
    class someComponent extends React.Component {
        constructor (props) {
            super(props);
            this.state = {
                redditItems: [];
            }
        }
    
        componentWillMount = () => {
            axios.get(API_URL).then(res => {
                this.setState({ redditItems: res.data.data.children });
            }).catch(err => {
                // CODE WHEN FAILS.
            });
        }
    
        renderList = () => {
            return this.state.reditItems.map((item, i) => (
                <div key={ i }>
                    <p>{ item.data.url }</p>
                    <p>{ item.data.author }</p>
                </div> // HOW YOU WANT TO DISPLAY YOUR ITEM
            ));
        }
    
        render = () => {
            return (
                <div>
                    { this.renderList() }
                </div>
            )
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 谢谢,但我没有使用 React。我的问题是我是否更适合为此使用 React 之类的东西,或者当我可以在 vanilla JS 中完成所有操作时,这是否不必要。
    • 抱歉造成误会。您绝对可以像使用 fetch api 一样执行此操作。我建议阅读this(如果你还没有这样做的话)。对于您遇到的性能问题,在调用 api 之前渲染占位符数据总是一个好主意。因此,当它加载时,它将被您的真实数据替换。毕竟我认为开始学习像 Vue 或 React 这样的框架并不是一个坏主意。你会惊讶于工作的乐趣。
    • 感谢您的回复!我认为您编辑了您的评论,但我已将 fetch 改为支持 async/await。当我有时间时,我会适当地研究反应。
    猜你喜欢
    • 2012-12-28
    • 2019-02-23
    • 1970-01-01
    • 2017-01-04
    • 2021-09-06
    • 2019-03-03
    • 2023-01-29
    • 2013-12-03
    • 1970-01-01
    相关资源
    最近更新 更多