Canceling animations with cancelAnimationFrame
The useEffect hook we’ve written has commandToAnimate and isDrawingLine in its dependency list. That means that when either of these values updates, the useEffect hook is torn down and will be restarted. But there are other occasions when we want to cancel the animation. One time this happens is when the user resets their screen.
If a command is currently animating when the user clicks the Reset button, we don’t want the current animation frame to continue. We want to clean that up.
Let’s add a test for that now:
- Add the following test at the bottom of
test/Drawing.test.js:it("calls cancelAnimationFrame on reset", () => { Â Â renderWithStore(<Drawing />, { Â Â Â Â script: { drawCommands: [horizontalLine] } Â Â }); Â Â renderWithStore(<Drawing />, { Â Â Â Â script: { drawCommands: [] } Â Â }); Â Â expect(window...