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

i want to select top 8 photos from jsonplaceholder api and then fetch them using reactjs actually i

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
    });
  });
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744258625a4565518.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信