【问题标题】:How to preview posts on single page如何在单页上预览帖子
【发布时间】:2022-01-10 11:46:40
【问题描述】:

嗨,

这是我在 react js 中的代码

我想在单击阅读更多按钮时在单个页面上显示我的博客文章我从中选择此网址 新闻 api,我想通过它的特定 id 显示每个罐子,但我使用 url 作为 id

import React, { Component } from "react";
import BlogPage from "./BlogPage";
import propTypes from "prop-types";
import InfiniteScroll from "react-infinite-scroll-component";

export default class blogPost extends Component {
  static defaultProps = {
    category: "general",
  };
  static propTypes = {
    category: propTypes.string,
  };

  constructor(props) {
    super(props);
    this.state = {
      articles: [],
    };
    /* document.title = `${this.props.category}-news `; */
  }

  async componentDidMount() {
    let url = 
    let data = await fetch(url);
    let parsData = await data.json();
    console.log(parsData);
    this.setState({
      articles: parsData.articles,
      totalResults: parsData.totalResults,
    });
  }

  getMorePost = async () => {
    let url = 
    let data = await fetch(url);
    let parsData = await data.json();
    console.log(parsData);
    this.setState({ articles: this.state.articles.concat(parsData.articles) });
  };

  render() {
    return (
      <div>
        <div className="container">
          {/* <span style={{ color: "grey" }}>
                Blogs 
                <span style={{ fontWeight: "500", color: "black" }}>
                  - {this.props.category}
                </span>
              </span> */}

          <InfiniteScroll
            dataLength={this.state.articles.length}
            next={getMorePost}
            hasMore={this.state.articles.length !== this.state.totalResults}
            loader={<h3> Loading...</h3>}
            endMessage={<h4>Nothing more to show</h4>}
          >
            <div className="row pt-3.">
              {this.state.articles.map((element) => {
                return (
                  <div className={this.props.BlogClass} key={element.url}>
                    <BlogPage
                      CardClass={this.props.ClassCard}
                      ClassDes={this.props.DesClass}
                      ClassTitle={(this.props.TitleClass, " heading-blog")}
                      CardBody={this.props.BodyCard}
                      imgStyle={this.props.styleImg}
                      CardStyle={{
                        width: "100%",
                        height: "100%",
                        padding: "10px 0",
                        border: "none",
                      }}
                      title={element.title ? element.title.slice(0, 45) : ""}
                      description={
                        element.description
                          ? element.description.slice(0, 90)
                          : ""
                      }
                      author={
                        /* element.author? element.author.slice(0, 5) : "" */
                        element.category
                      }
                      img={element.urlToImage}
                    />
                  </div>
                );
              })}
            </div>
          </InfiniteScroll>
        </div>
      </div>
    );
  }
}

我想通过点击阅读更多按钮在此页面上显示此帖子我尝试了几次但不明白

import React, { useState, useEffect } from "react";
import BlogPost from "./BlogPost";

export default function Blogpreview() {
  /* const [blog,setBlog]=useState()


    useEffect(() => {
        let blog = articles.find((blog) => blog.id === parseInt(id));
        if (blog) {
          setBlog(blog);
        }
    }, []) */

  /* const [blog,setBlog]=useState(null)


    useState = async () =>{
        let url = 
    let data = await fetch(url);
    let parsData = await data.json();
    console.log(parsData);
    setBlog({ articles: parsData.articles,
      totalResults: parsData.totalResults
     });
    }
 */

  return (
    <div className="container-fluid">
      <div className="col-6 BlogPreviewBox px-5 ">
        <div className="d-flex justify-content-center">
          <img className="display-blog-box my-5" src="/"></img>
        </div>
        <h1 className="blog-preview-heading">
          Why invoicing are beneficial for your business{/* {blog.title} */}
        </h1>
        <div className="d-flex flex-column align-items-center">
          <span className="text-center py-3">07 December 2021</span>
          <button className="btn-blog-display">2 min read</button>
        </div>
      </div>

      <div className="row display-blog-content-row">
        {/*  <div className="col-3">
               { <div>
                    <p >What is Olabooks?</p>
                    <div className="d-flex flex-column">
                        <span>How to add invoice in Olabooks?</span>
                        <span className="py-2">How to share invoice in Olabooks?</span>
                    </div>
                    <i className="far fa-heart"></i>
                </div>}
            </div> */}
        <div className="col-6">
          <div className="d-flex flex-column display-blog-text">
            <div>
              <span>
                Businesses that adopt advanced technologies are more likely to
                thrive in the long run because of the rapid response to modern
                techniques and innovation. Many questions need to be taken into
                account while adopting advanced technology, but most
                importantly, you begin to think about the values it must provide
                to you and your business. It is not an easy task toruna
                business, especially in this tech-savvy world. Let's dig into
                some commercial benefits of moving from pen and paper invoicing
                to efficient e-invoicing that would make headway for your
                business.
              </span>
            </div>
          </div>
          <div className="tag-display-blog">
            <div>
              <strong>By Olabooks</strong>
              <span>author</span>
            </div>
          </div>
        </div>
        {/*  <div className="col-3">
                <div>
                    <span>Related</span>
                </div>
                <div className="displa-related-blog">
                    <h5 className="py-2">Why is e-invoicing beneficial for your business?</h5>
                    <span>
                        Business that adopt advanced technologies are more likely to thrive in the long run because of
                    </span>
                    <button className="btn-related-blog-display">9 min read</button>
                </div>
            </div> */}
      </div>
    </div>
  );
}

【问题讨论】:

  • 不清楚是什么问题。 “阅读更多”按钮在哪里? BlogPreview 与 BlogPost 有什么关系?

标签: javascript reactjs next.js


【解决方案1】:

我尝试对同一个 URL 进行 API 调用,您似乎在两种情况下都获得了相同的 API 响应(使用和不使用 pageSize 参数),因此,您的“阅读more" 实际上并没有在屏幕上呈现任何新内容

(使用 pageSize 查询参数)

(不带pageSize参数)

另外,最好不要在问题中包含您的 API 密钥

【讨论】:

  • 如果您指的是totalResults,那么这是与pageSize 值无关的文章总数。如果您检查返回的articles 的实际数量,您会看到第一次调用中只有 20 个项目(pageSize 值)。
猜你喜欢
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
相关资源
最近更新 更多