最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - select top 8 rows from jsonPlaceHolder fake data with reactJs - Stack Overflow

programmeradmin4浏览0评论

i want to select top 8 photos from jsonplaceholder api and then fetch them using reactjs actually i'm using axios and here's my code:

class App extends React.Component {

  ponentDidMount() {
    axios.get(`/`)
      .then(res => {
        const pictures = res.data;
        this.setState({
          pictures,
          loading: false,
          error: null
        });
      })
      .catch(err => {
        this.setState({
          loading: false,
          error: err
        });
      });
  }

  renderLoading() {
    return <div>Loading...</div>;
  }

  renderError() {
    return (
      <div>
        Something went wrong: {this.state.error.message}
      </div>
    );
  }

i want to select top 8 photos from jsonplaceholder api and then fetch them using reactjs actually i'm using axios and here's my code:

class App extends React.Component {

  ponentDidMount() {
    axios.get(`https://jsonplaceholder.typicode./photos/`)
      .then(res => {
        const pictures = res.data;
        this.setState({
          pictures,
          loading: false,
          error: null
        });
      })
      .catch(err => {
        this.setState({
          loading: false,
          error: err
        });
      });
  }

  renderLoading() {
    return <div>Loading...</div>;
  }

  renderError() {
    return (
      <div>
        Something went wrong: {this.state.error.message}
      </div>
    );
  }

Share Improve this question asked May 18, 2018 at 2:34 elmaelma 211 silver badge5 bronze badges 1
  • 1 OK, awesome. But did you have a question? – Randy Casburn Commented May 18, 2018 at 2:59
Add a ment  | 

2 Answers 2

Reset to default 5

There is a simple way you can limit data fetching is by => "http://jsonplaceholder.typicode./photos?_start=0&_limit=5" It means, starting at 0 and goes up to 5.

Api does not provide a way to limit number of records to return so you can save only required number of records by using Array.prototype.slice(start, end)

ponentDidMount() {
  axios.get(`https://jsonplaceholder.typicode./photos/`)
  .then(res => {
    const pictures = res.data.slice(0, 8);
    this.setState({
      pictures,
      loading: false,
      error: null
    });
  })
  .catch(err => {
    this.setState({
      loading: false,
      error: err
    });
  });
}
发布评论

评论列表(0)

  1. 暂无评论