简体   繁体   English

如何每隔X秒更改状态

[英]How to change state every x seconds

I need to change the name of every avatar every X seconds, I have follow this solution and it works fine but, right now its changing all the names to the same name from RandomAcidName array. 我需要每X秒钟更改每个头像的名称,我已经按照此解决方案进行了工作,但是它可以正常工作,但是现在,它会将所有名称从RandomAcidName数组更改为相同的名称。

I imagine that I need to iterate through this list also, so each name is passing just once to every avatar. 我想我也需要遍历此列表,因此每个名称仅传递一次给每个化身。

Here is my code 这是我的代码

nameJuggler is passing the name to each avatar nameJuggler将名称传递给每个头像

import React, { Component } from "react";
import "./Avatar.scss";
import AcidData from "../Acidsdata/Acids-data.json";
import RandomAcidName from "../../../src/randomAcidName.json";

const getDate = new Date();
const getDay = getDate.getDate();
const getMonth = getDate.getMonth() + 1;
const fullTodayDate = getDay + "/" + getMonth;

const AvatarDisplay = props => {
  return (
    <div key={props.id} className="container__position-relative">
      <img src={props.photo} alt={props.name} className="avatar__img" />
      <div className="container__position-absolute avatar__name">
        <span>{props.name}</span>
      </div>
      {props.cumple}
    </div>
  );
};

class AcidPoblation extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sudoNameId: 0,
      acidos: AcidData,
      randomName: RandomAcidName
    };
  }

  componentDidMount() {
    this.changeName = setInterval(() => {
      let currentSudoNameId = this.state.sudoNameId;

      this.setState({
        sudoNameId: currentSudoNameId + 1
      });
    }, 1500);
  }

  componentDidUnmount() {
    clearInterval(this.changeName);
  }

  render() {
    let nameJuggler =
      RandomAcidName[this.state.sudoNameId % RandomAcidName.length];

    return (
      <main className="container__grid">
        {this.state.acidos.map(item => (
          <AvatarDisplay
            photo={item.acidphoto}
            name={nameJuggler}
            birth={item.birthdate}
            id={item.acidid}
            cumple={
              item.birthdate === fullTodayDate ? (
                <img
                  src="https://via.placeholder.com/40x40"
                  alt="feliz cumpleaños"
                  className="container__position-absolute avatar__birth-date"
                />
              ) : null
            }
          />
        ))}
      </main>
    );
  }
}

export default AcidPoblation;

Here is the Json with the names 这是杰森的名字


[

  "Anderson",
  "Ashwoon",
  "Aikin",
  "Bateman",
  "Bongard",
  "Bowers",
  "Boyd",
  "Cannon",
  "Cast",
  "Deitz",
  "Dewalt",
  "Ebner",
  "Frick",
  "Hancock",
  "Haworth",
  "Hesch",
  "Hoffman",
  "Kassing",
  "Knutson",
  "Lawless",
  "Lawicki",
  "Mccord",
  "McCormack",
  "Miller",
  "Myers",
  "Nugent",
  "Ortiz",
  "Orwig",
  "Ory",
  "Paiser",
  "Pak",
  "Pettigrew",
  "Quinn",
  "Quizoz",
  "Ramachandran",
  "Resnick",
  "Sagar",
  "Schickowski",
  "Schiebel",
  "Sellon",
  "Severson",
  "Shaffer",
  "Solberg",
  "Soloman",
  "Sonderling",
  "Soukup",
  "Soulis",
  "Stahl",
  "Sweeney",
  "Tandy",
  "Trebil",
  "Trusela",
  "Trussel",
  "Turco",
  "Uddin",
  "Uflan",
  "Ulrich",
  "Upson",
  "Vader",
  "Vail",
  "Valente",
  "Van Zandt",
  "Vanderpoel",
  "Ventotla",
  "Vogal",
  "Wagle",
  "Wagner",
  "Wakefield",
  "Weinstein",
  "Weiss",
  "Woo",
  "Yang",
  "Yates",
  "Yocum",
  "Zeaser",
  "Zeller",
  "Ziegler",
  "Bauer",
  "Baxster",
  "Casal",
  "Cataldi",
  "Caswell",
  "Celedon",
  "Chambers",
  "Chapman",
  "Christensen",
  "Darnell",
  "Davidson",
  "Davis",
  "DeLorenzo",
  "Dinkins",
  "Doran",
  "Dugelman",
  "Dugan",
  "Duffman",
  "Eastman",
  "Ferro",
  "Ferry",
  "Fletcher",
  "Fietzer",
  "Hylan",
  "Hydinger",
  "Illingsworth",
  "Ingram",
  "Irwin",
  "Jagtap",
  "Jenson",
  "Johnson",
  "Johnsen",
  "Jones",
  "Jurgenson",
  "Kalleg",
  "Kaskel",
  "Keller",
  "Leisinger",
  "LePage",
  "Lewis",
  "Linde",
  "Lulloff",
  "Maki",
  "Martin",
  "McGinnis",
  "Mills",
  "Moody",
  "Moore",
  "Napier",
  "Nelson",
  "Norquist",
  "Nuttle",
  "Olson",
  "Ostrander",
  "Reamer",
  "Reardon",
  "Reyes",
  "Rice",
  "Ripka",
  "Roberts",
  "Rogers",
  "Root",
  "Sandstrom",
  "Sawyer",
  "Schlicht",
  "Schmitt",
  "Schwager",
  "Schutz",
  "Schuster",
  "Tapia",
  "Thompson",
  "Tiernan",
  "Tisler"

]


I need that every name is assing just once. 我需要每个名字都只问一次。

In this link is a replica of my project... 在此链接中是我的项目的副本...

The problem is 问题是

let nameJuggler = [this.state.sudoNameId % RandomAcidName.length];

this.state.sudoNameId won't be unique because it's just a number set in the parent component and will be provided to all of the AvatarDisplays not dynamically changing. this.state.sudoNameId不会是唯一的,因为它只是在父组件中设置的数字,并且将提供给所有不动态更改的AvatarDisplays。 An easy fix would just make the .map also have an index so you can add the index to the this.state.sudoNameId which will make each number unique. 一个简单的解决方法就是使.map也具有索引,因此您可以将索引添加到this.state.sudoNameId中,这将使每个数字唯一。 See code below 见下面的代码

    return (
      <main className="container__grid">
        {this.state.acidos.map((item, index) => {
            let nameJuggler =
            RandomAcidName[(this.state.sudoNameId + index) % RandomAcidName.length];
          return (
          <AvatarDisplay
            photo={item.acidphoto}
            name={nameJuggler}
            birth={item.birthdate}
            id={item.acidid}
            cumple={
              item.birthdate === fullTodayDate ? (
                <img
                  src="https://via.placeholder.com/40x40"
                  alt="feliz cumpleaños"
                  className="container__position-absolute avatar__birth-date"
                />
              ) : null
            }
          />
        )
        })}
      </main>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM