简体   繁体   English

ReactJS变量更改未呈现元素

[英]ReactJS variable change isn't rendering element

I have a fairly simple situation (I'm using ReactJS with A-Frame / A-Frame React, but that shouldn't really affect anything here). 我有一个相当简单的情况(我将ReactJS与A-Frame / A-Frame React一起使用,但这实际上不会影响到这里)。

I have an entity that hints to a user that they can click / swipe an object. 我有一个实体,向用户暗示他们可以单击/滑动对象。 I want that entity to disappear after the user has clicked on said object, and then reappear after 10 seconds. 我希望该实体在用户单击该对象后消失,然后在10秒后重新出现。

I'm relatively new to React so may be doing something wrong here: 我是React的新手,因此在这里可能做错了什么:

  1. At the top of my JS file (after my imports, but before the class begins, I'm doing let showHinter = true; 在我的JS文件的顶部(在我的导入之后,但在class开始之前,我正在执行let showHinter = true;

  2. I've got an entity in my render() method like this: 我的render()方法中有一个实体,如下所示:

     <AFrameEntity events={{ click: (ev) => { showHinter = false; setTimeout(() => { console.log("showHinter Value:", showHinter) /* ↑ This fires, but ↓ this doesn't do anything */ showHinter = true; console.log("showHinter Value:", showHinter) }, 5000) } }} > </AFrameEntity> 
  3. My "hinter" component is a sibling to that, like so: 我的“ hinter”组件与此类似,例如:

     { <AFrameEntity /* Bunch of irrelevant stuff here */ > // fade out { !showHinter && <a-animation attribute="material.opacity" dur="1000" fill="forwards" from="1" to="0" repeat="0"></a-animation> } //fade in { showHinter && <a-animation attribute="material.opacity" dur="1000" fill="forwards" from="0" to="1" repeat="0"></a-animation> } </AFrameEntity> } 

When clicking, showHinter is correctly set to false and the element disappears. 单击时, showHinter正确设置为false并且该元素消失。 However, it never re-appears. 但是,它再也不会出现。 My console logs do happen, and showHinter === false , yet my fade-in animation never happens, and inspecting the element with react's devtools shows my fade-out animation entity, and NOT my fade-in one (which should be triggered when showHinter === true . 我的控制台日志确实发生了,并且showHinter === false ,但是我的淡入动画从未发生,并且使用react的devtools检查元素显示了我的淡出动画实体,而不是我的淡入动画实体(当showHinter === true

So the basic question is, why isn't React react-ing to my variable change? 所以基本的问题是,为什么React不对我的变量更改做出反应? Do I need to force it to re-render somehow? 我是否需要强迫它以某种方式重新渲染?

You should do every change, that effect the view or where you expect a re-render, via state. 您应该通过状态进行所有更改,以影响视图或期望重新渲染的位置。

In your case you do something like(not tested): 在您的情况下,您可以执行以下操作(未经测试):

<AFrameEntity
events={{
  click: (ev) => {

    this.setState({showHinter: false)};

    setTimeout(() => {
      console.log("showHinter Value:", this.state.showHinter)

      /* ↑ This fires, but ↓ this doesn't do anything */

      this.setState({showHinter: true)};
      console.log("showHinter Value:", this.state.showHinter)
    }, 5000)

  }
}}

And check the state in your render function. 并检查渲染功能中的状态。

{

  <AFrameEntity /* Bunch of irrelevant stuff here */ >

    // fade out
    {
     !this.state.showHinter && 
      <a-animation 
        attribute="material.opacity"
        dur="1000"
        fill="forwards"
        from="1"
        to="0"
        repeat="0"></a-animation>
    }
      //fade in
    {
     this.state.showHinter && 
      <a-animation 
        attribute="material.opacity"
        dur="1000"
        fill="forwards"
        from="0"
        to="1"
        repeat="0"></a-animation>
    }
  </AFrameEntity>
}

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

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