globocom / react-native-draftjs-render

React Native render for draft.js model
MIT License
390 stars 62 forks source link

Warning : Can't call setState (or forceUpdate) on an unmounted component. #46

Closed philohelp closed 5 years ago

philohelp commented 6 years ago

Hi all,

I've been playing around for a while with the rn-draft-render and, while everything works perfect in general, there's one small thing that I can't seem to do right while trying to embed content. I always get the warning "Can't call setState (or forceUpdate) on an unmounted component." I could live with this warning but, you know...

Here is the use case : 1- My draft contentState contains an "atomic block". 2- From this block (and from its corresponding entity) I retrieve a url. 3- I then regex the url : if it's an image I show Image, if it's a video a webview with an embed player etc. 4- Until then, everything works fine. 5- If it's not an image or a vid, I use a smart little API to retrieve the content of the webpage (title, desc, favicons, images) and to show it in a PagePreview component. 6- I fetch the classic way : my pagePreview component has a componentWillMount function and when data is retrieved it setStates the data and then the comp re-renders accordingly. 7- It works... except the warning I keep getting when the comp rerenders. What I dont understand is that my comp IS ALREADY MOUNTED. I'm not the kind of guy to call setState on an unmounted component, I would never do something like that. 8- I've tried to fetch the api from inside the atomic function but without success.

Would this be a problem with any stateful / changing state component ?

philohelp commented 6 years ago

The only strategy I can think of would be to do all the fetch work from inside the getBlocks function and pass all the resulting data as props. I wonder if it worth it and for what result.

raphaelpor commented 6 years ago

Hello @philohelp! This error happens when you call setState on a Component that is not mounted on the screen. Generally for two reasons: when you use Flatlist and when you close the screen, going back to the previous screen.

raphaelpor commented 6 years ago

One possible fix is to cancel the Promise on componentWillUnmount().