等到`this.state`在运行功能之前设置了一段时间

时间:2017-05-03 03:39:39

标签: javascript reactjs fetch

我有一个按钮,它有3个状态。根据触发的状态,应该进行提取以发布此数据。

基本上,我希望它等到this.state.favourite值设置超过200毫秒。然后它应该触发获取。

它永远不应发布多个提取,

我尝试使用lodash的_.debounce,但它没有任何影响。它仍然立即运行该功能。

我也把它放在CodePen

class Switch extends React.Component {
  constructor() {
    super();
    this.state = {
       favourite: 0
    }
  }

        handleClick() {
    this.setState((prevState) => ({
       favourite: (prevState.favourite + 1) % 3
    }));
    return _.debounce(this.favChosen(), 1000)
  }
  favChosen(){
    if (this.state.favourite === 0) {
    return this.testConsole1();
  } else if (this.state.favourite === 1) {
    return this.testConsole2();
  } else if (this.state.favourite === 2) {
    return this.testConsole3();
  }
 testConsole1() {
    console.log('This will be a fetch 1')
  }
  testConsole2() {
    console.log('This will be a fetch 2')
  }
  testConsole3() {
    console.log('This will be a fetch 3')
  }
  render () {
    const { favourite } = this.state;
    const fill = favourite === 0 ? "grey" :
                 favourite === 1 ? "green" : "red";
    return (
        <button className="favStar" onClick={this.handleClick.bind(this)} >
      <svg width="100" height="100">
        <g>
          <path id="svg_2" d="m0,38l37,0l11,-38l11,38l37,0l-30,23l11,38l-30,-23l-30,23l11,-38l-30,-23l0,0z" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" fill={fill} />
        </g>
      </svg>
            </button>
    );
  }
}

React.render( <Switch />, document.getElementById( "page" ) );

2 个答案:

答案 0 :(得分:2)

您没有正确使用debounce

  constructor() {
    super();
    this.state = {
       favourite: 0
    }

    this.favChosen = _.debounce(this.favChosenRaw, 1000);     
  }

  handleClick() {
    this.setState((prevState) => ({
       favourite: (prevState.favourite + 1) % 3
    }));

    this.favChosen()

  }
  favChosenRaw(){....

工作小提琴:

http://codepen.io/anon/pen/GmELKo?editors=1010

答案 1 :(得分:1)

触发状态更改操作的更好解决方案是在setState回调上调用该函数,而不是在调用函数之前等待固定时间。你永远不知道改变状态需要多长时间,等待足够长的时间会限制你的应用程序。试试这个

class Switch extends React.Component {
  constructor() {
    super();
    this.state = {
       favourite: 0
    }
  }

  handleClick() {
    this.setState((prevState) => ({
       favourite: (prevState.favourite + 1) % 3
    }), () => {this.favChosen()});

  }
  favChosen(){
    if (this.state.favourite === 0) {
    return this.testConsole1();
  } else if (this.state.favourite === 1) {
    return this.testConsole2();
  } else if (this.state.favourite === 2) {
    return this.testConsole3();
  }
 testConsole1() {
    console.log('This will be a fetch 1')
  }
  testConsole2() {
    console.log('This will be a fetch 2')
  }
  testConsole3() {
    console.log('This will be a fetch 3')
  }
  render () {
    const { favourite } = this.state;
    const fill = favourite === 0 ? "grey" :
                 favourite === 1 ? "green" : "red";
    return (
        <button className="favStar" onClick={this.handleClick.bind(this)} >
      <svg width="100" height="100">
        <g>
          <path id="svg_2" d="m0,38l37,0l11,-38l11,38l37,0l-30,23l11,38l-30,-23l-30,23l11,-38l-30,-23l0,0z" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" fill={fill} />
        </g>
      </svg>
            </button>
    );
  }
}

React.render( <Switch />, document.getElementById( "page" ) );

<强> CODEPEN