22

I want to destroy some component that i have instantiated to release memory. In my current app almost every view that i instantiate and then release it (remove reference to it) doesn't get garbage collected. I keep no reference to to views. I'm not sure if this memory leak is caused by my app or it's react-native(and react native have some memory leaks problems). is there a way to confidently destroy a view instance ?

Sinan Yaman
  • 5,714
  • 2
  • 15
  • 35
Hamid Karimi
  • 615
  • 1
  • 8
  • 12

2 Answers2

1

React will destroy a component when you don't render it anymore. or when you omit it from virtual DOM.

const [render, setRender] = useState(true)

<View>
   {render
     ? <HeavyComponent/>    
     : null
   }
   <AnotherComponent/>
</View>
TheEhsanSarshar
  • 2,677
  • 22
  • 41
0

I've passed for the same problem time ago, and I discovered that the problem was I wasn't using correctly react.

Why are you instantiating components manually?

Think that one of big main features of reacts is the tree DOM virtual components and if you instantiate a component manually you are, in some way, avoiding it.

Remember that you should use the components in the render function, or functions where components are render and used in the render function. If you have to pass components to another components, you should use the concept of high order component.

I hope I've helped you.

Lucas Milotich
  • 370
  • 3
  • 15