反应:如果在请求动画帧中设置了状态,会在动画帧上反应计划渲染吗?

时间:2018-12-30 16:22:01

标签: reactjs requestanimationframe

我试图理解在React(版本16+)中使用游戏循环之类的细微差别。对于React的渲染策略如何与另一个渲染计划程序发生冲突(或不发生冲突),我感到困惑-在这种情况下:请求动画帧。

请参考以下示例,其中使用游戏循环来设置状态:

class Loop extends Component {
  constructor(props) {
    super(props);
    this.state = { x: 0 };
  }

  componentDidMount() {
    let then = performance.now();
    const loop = now => {
      if (this.state.x < 400)
        window.requestAnimationFrame(loop);
      const dt = now - then;
      then = now;
      this.setState(prevState => ({ x: prevState.x + (dt * 0.1) }));
    };
    loop(then);
  }

  render() {
    const { x } = this.state;
    return <div style={{
      backgroundColor: "green",
      height: "50px",
      width: `${x}px`
    }}></div>;
  }
}

这项工作是否类似于直接操纵DOM的工作?还是会做出反应,例如执行批处理状态更新来渲染,从而违反了使用请求动画帧的目的?

1 个答案:

答案 0 :(得分:3)

这就是我在回答以下问题时的想法:How to implement a gameloop with requestAnimationFrame across multiple React Redux components?

setState is asynchronous一样,实际上不能保证在调用它时React将执行更新并重新渲染组件,相反,React可以将更新推送到一个队列中,该队列将在某个时候处理。点,它实际上可能在下一帧或以后的帧中。

我使用requestAnimationFrame剖析了一个非常简单的应用程序,以查看情况是否如此,实际上并非如此:

class ProgressBar extends React.Component {

  constructor(props) {
    super(props);
    
    this.state = {
      progress: 0,
    };
  }
  
  update() {
    this.setState((state) => ({
      progress: (state.progress + 0.5) % 100,
    }));
  }  

  render() {
    const { color } = this.props;
    const { progress } = this.state;
    
    const style = {
      background: color,
      width: `${ progress }%`,
    };
    
    return(
      <div className="progressBarWrapper">
        <div className="progressBarProgress" style={ style }></div>
      </div>
    );  
  }
}

class Main extends React.Component {

  constructor(props) {
    super(props);
    
    const progress1 = this.progress1 = React.createRef();
    const progress2 = this.progress2 = React.createRef();
    const progress3 = this.progress3 = React.createRef();
    
    this.componentsToUpdate = [progress1, progress2, progress3];
    this.animationID = null;    
  }
  
  componentDidMount() {  
    this.animationID = window.requestAnimationFrame(() => this.update());  
  }
  
  componentWillUnmount() {
    window.cancelAnimationFrame(this.animationID);
  }
  
  update() {
    this.componentsToUpdate.map(component => component.current.update());
  
    this.animationID = window.requestAnimationFrame(() => this.update());  
  }
  
  render() {
    return(
      <div>
        <ProgressBar ref={ this.progress1 } color="magenta" />
        <ProgressBar ref={ this.progress2 } color="blue" />     
        <ProgressBar ref={ this.progress3 } color="yellow" />       
      </div>
    );
  }
}

ReactDOM.render(<Main />, document.getElementById('app'));
body {
  margin: 0;
  padding: 16px;
}

.progressBarWrapper {
  position: relative;
  width: 100%;
  border: 3px solid black;
  height: 32px;
  box-sizing: border-box;
  margin-bottom: 16px;
}

.progressBarProgress {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
}
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>

<div id="app"></div>

在这里您可以看到如何将更新添加到队列(enqueueSetState),但是工作是立即完成的,所有对updatesetState,{{1 }},render ...发生在同一帧中:

Example app profiling result

但是,我怀疑在一个实际的应用程序中,React可以处理更多的更新,或者在将来的React版本中具有时间分片等功能,具有优先级的异步更新...实际上可能不是这种情况。

相关问题