苦恼于如何最好地处理它。我可以使用requestAnimationFrame
的递归调用来实现游戏循环:
export interface Props {
name: string;
points: number;
onIncrement?: () => void;
onDecrement?: () => void;
}
class Hello extends React.Component<Props, object> {
constructor(props: Props) {
super(props);
}
render() {
const { name, points, onIncrement, onDecrement } = this.props;
return (
<div className="hello">
<div className="greeting">
Hello {name + points}
</div>
<button onClick={onDecrement}>-</button>
<button onClick={onIncrement}>+</button>
</div>
);
}
componentDidMount() {
this.tick();
}
tick = () => {
this.props.onIncrement();
requestAnimationFrame(this.tick)
}
}
但是如果我想在每个帧上执行以下操作:
- Component1 执行 X 操作
- Component2 执行 Y 操作
- Component3 执行 Z 操作
我可以在每个组件中添加另一个循环,但是我的理解是同时运行多个 requestAnimationFrame
循环是不好的实践,并且会对性能造成重大影响。
所以我有些困惑。如何使另一个组件使用相同的循环?(如果这甚至是最好的方法!)