te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to change a react state immediately by setTimeout after setState? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change a react state immediately by setTimeout after setState? - Stack Overflow

programmeradmin3浏览0评论

new in react here, don't know if it's right to do this on the setState callback like this?

           setTimeout(()=> {
            this.setState((state, props) => ({ activateLightColorForRed: true }), () => {
              setTimeout(()=> {
                this.setState(()=> ({ activateLightColorForRed: false }))
              }, 500);
              red.play();
            })
          }, toWait); 

or maybe something like this?

 this.setState((state, props) => {
    this.setState((state, props) => {
      activateLightColorForRed: true
    });
    setTimeout(() => { activateLightColorForRed: false },500)
  })

are the state on the setState callback updated? something weird is happening in my ponents, it's rendering multiple times. I am not sure but I think it's because I'm doing the first sample?

new in react here, don't know if it's right to do this on the setState callback like this?

           setTimeout(()=> {
            this.setState((state, props) => ({ activateLightColorForRed: true }), () => {
              setTimeout(()=> {
                this.setState(()=> ({ activateLightColorForRed: false }))
              }, 500);
              red.play();
            })
          }, toWait); 

or maybe something like this?

 this.setState((state, props) => {
    this.setState((state, props) => {
      activateLightColorForRed: true
    });
    setTimeout(() => { activateLightColorForRed: false },500)
  })

are the state on the setState callback updated? something weird is happening in my ponents, it's rendering multiple times. I am not sure but I think it's because I'm doing the first sample?

Share Improve this question asked Sep 11, 2017 at 21:13 gpbaculiogpbaculio 5,96814 gold badges68 silver badges112 bronze badges 2
  • 1 could you post the full code for the ponent? Are you calling setState from within the render method? – James Gentes Commented Sep 11, 2017 at 22:26
  • 1 Heads-up @i-am-newbie : setState are asynchronous in nature and you could also use simply this.setState({activateLightColorForRed:true}) i.e. pass a object instead of passing in a function. Might help in readability. And yeah post the plete code for the ponent. It will help us. Thanks – KeshavDulal Commented Sep 12, 2017 at 3:23
Add a ment  | 

1 Answer 1

Reset to default 12

Your question does not seem to follow the pattern of a regular react app. You should be using the lifecycle events to react to the state being changed. You should not be making multiple, nested, confusing callbacks (like it seems you want to do).

Might I suggest a structure more like this

class Foo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      activateLightColorForRed: false,
    };
  }
  ponentDidUpdate(prevProps, prevState) {
    if (this.state.activateLightColorForRed) {
      // when the state is updated (turned red), 
      // a timeout is triggered to switch it back off
      this.turnOffRedTimeout = setTimeout(() => { 
        this.setState(() => ({activateLightColorForRed: false}))
      }, 500);
    }
  }
  ponentWillUnmount() {
    // we set the timeout to this.turnOffRedTimeout so that we can
    // clean it up when the ponent is unmounted.
    // otherwise you could get your app trying to modify the state on an
    // unmounted ponent, which will throw an error
    clearTimeout(this.turnOffRedTimeout);
  }
  render () {
    // really simply ui to show how it *could* work
    return (
      <div onClick={this.turnLightRed}>
        The light is {this.state.activateLightColorForRed ? "Red" : "Green"}.
        <br /> Click to change!
      </div>
    )
  }
  turnLightRed = () => {
    // this function will turn the light red
    this.setState(() => ({ 
      activateLightColorForRed: true 
    }));
  }
}

ReactDOM.render(
  <Foo name="World" />,
  document.getElementById('container')
);
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.0.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.0.0/react-dom.min.js"></script>
<div id="container"></div>

Hope that helps.

发布评论

评论列表(0)

  1. 暂无评论