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 ?
Asked
Active
Viewed 5,272 times
22
-
2Did you find the answer? – abeikverdi Aug 03 '16 at 08:25
-
I'm also interested in this. Did you or @abeikverdi find any insight? – Noitidart Mar 12 '17 at 01:42
-
UPDATE: this question is for early release of react-native and android <5, this problem has long time ago has been solved. – Hamid Karimi Oct 12 '18 at 10:14
2 Answers
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
-
A trick if you want to manipulate a little bit the component, you can use the function componentWillUnmount(). – Lucas Milotich Oct 02 '17 at 18:39