简体   繁体   English

对于 reactJS 中的标签,如何在间隔内返回值

[英]how to return a value inside an interval, for a tag in reactJS

I have this code, and i want to port it into a tag in my reactJS app.我有这段代码,我想将它移植到我的 reactJS 应用程序中的标签中。 What it's suppose to do is slowly update the lets to reveal my name.它应该做的是慢慢更新让我们透露我的名字。 Kinda like it's being decrypted.有点像被解密了。

However it does not seem to return anything, the logic works, since the console.log inside the interval does what it needs to do.然而,它似乎没有返回任何东西,逻辑有效,因为间隔内的 console.log 做了它需要做的事情。 But anything outside the interval does not.但是间隔之外的任何东西都没有。 Can someone help me understand how to get this to work for a html tag in my react app?有人可以帮我理解如何让它在我的反应应用程序中为 html 标签工作吗?


   <h3 className="center-align" id="my_name">
       {this.hackerLoad()}
   </h3>
//^^^^^^^ this is the small section i need to edit,
// i didn't want to add a bunch of non relevant code. this is inside my return(); 



   hackerLoad = () => {
        console.log("loaded");
        let hiddenName = [
            "Dwkuow Flexul",
            "Dwkuow Flexua",
            "Dwkuow Flexia",
            "Dwkuow Flecia",
            "Dwkuow Flrcia",
            "Dwkuow Farcia",
            "Dwkuow Garcia",
            "Dwkuoe Garcia",
            "Dwkupe Garcia",
            "Dwkipe Garcia",
            "Dwlipe Garcia",
            "Delipe Garcia",
            "Felipe Garcia",
        ];
        let i = 0;
        let name = "Dwkuow Flexul";
        const createDevName = setInterval(() => {
            i += 1;
            if (i > 12) {
                clearInterval(createDevName);
            }
            name = hiddenName[i];
            console.log(name);
        }, 500);

        return name;
    };

Expanding on my comment, here is how you can make this work.扩展我的评论,这里是如何使这项工作。 The key thing to remember is that React components only rerender when either their props or their state change.要记住的关键是 React 组件仅在其 props 或 state 更改时才会重新渲染。 Here is a working solution using state.这是使用 state 的工作解决方案。

Change the relevant section of render torender的相关部分更改为

<h3 className="center-align" id="my_name">
   {this.state.name}
</h3>

In the constructor, include this.state ={name: "Dwkuow Flexul"}在构造函数中,包含this.state ={name: "Dwkuow Flexul"}

And move the name updating logic to componentDidMount , using setState to update the name:并将名称更新逻辑移动到componentDidMount ,使用setState更新名称:

componentDidMount = () => {
    let hiddenName = [
        "Dwkuow Flexul",
        "Dwkuow Flexua",
        "Dwkuow Flexia",
        "Dwkuow Flecia",
        "Dwkuow Flrcia",
        "Dwkuow Farcia",
        "Dwkuow Garcia",
        "Dwkuoe Garcia",
        "Dwkupe Garcia",
        "Dwkipe Garcia",
        "Dwlipe Garcia",
        "Delipe Garcia",
        "Felipe Garcia",
    ];
    let i = 0;
    let name = "Dwkuow Flexul";
    const createDevName = setInterval(() => {
        i += 1;
        if (i > 12) {
            clearInterval(createDevName);
        }
        this.setState({name: hiddenName[i]});
    }, 500);
};

You should also clear the interval in componentWillUnmount , to avoid memory leaks in case of the component getting destroyed early.您还应该清除componentWillUnmount中的间隔,以避免 memory 泄漏,以防组件提前销毁。 (Which will need the interval id to be moved from the local scope of componentDidMount to a property of the entire component.) (这需要将区间 id 从 componentDidMount 的本地 scope 移动到整个componentDidMount的属性。)

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

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