Matter js cleanup and destroy instances

Posted on February 18, 2021

If you want to use a Matter instance in a React component, I bet that you need to destroy the instance when the component unmount.

Here the code to do that:

Render.stop(render)
World.clear(engine.world)
Engine.clear(engine)
render.canvas.remove()
render.canvas = null
render.context = null
render.textures = {}

Solution grabbed from here.