【发布时间】:2020-04-26 22:30:59
【问题描述】:
我有这段代码,我想将它移植到我的 reactJS 应用程序中的标签中。它应该做的是慢慢更新让我们透露我的名字。有点像被解密了。
但是它似乎没有返回任何东西,逻辑是有效的,因为间隔内的 console.log 做了它需要做的事情。但是间隔之外的任何东西都没有。有人可以帮我理解如何让它在我的 react 应用中为 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;
};
【问题讨论】:
-
仅仅改变闭包中的
name变量不会导致重新渲染。您需要将name的更改内容存储在组件的状态中。 -
我对 react 很陌生,我不确定你的意思。我知道返回名称没有得到实际名称/正在更新,因为它不在时间间隔的范围内。但我不确定如何实现它以便它在组件中生成
标签: javascript reactjs scope return setinterval