mardi 29 juin 2021

Randomize results onload React

i'm trying to randomize the results I receive from the API everytime I refresh the page. I´m a little bit stuck in which way to set up the onload.

This is my code

  handleRandom = (e) => {
    this.getMarvelResponse(null, null, e);
  };
  componentDidMount() {
    const random = Math.floor(Math.random() * 1473);
    this.getMarvelResponse(null, null, this.state.offset);
    onload = () => {this.handleRandom(random)}
  }

  getMarvelResponse = (id, name, offset) => {
    getCharacters({
      id: id,
      name: name,
      offset: offset,
    }).then(({ characters }) => {
      this.setState(
        {
          characters: characters.filter(
            (chr) => !chr.thumbnail.path.match(imageFound)
          ),      

          selectedCard: null,
          offset: offset,
          name: name,
        },
        () => {
          console.log(this.state.characters);
        }
      );
    });



Aucun commentaire:

Enregistrer un commentaire