microsoft / ReSub

A library for writing React components that automatically manage subscriptions to data sources simply by accessing them
MIT License
607 stars 49 forks source link

Problem with _buildState + componentDidMount in same component #136

Closed alariej closed 4 years ago

alariej commented 4 years ago

I just ported a small project to the latest versions of ReactXP and ReSub. The project dependencies are:

"@react-native-community/netinfo": "^3.2.1", "react": "16.12.0", "react-dom": "16.12.0", "react-native": "0.59.10", "react-native-windows": "0.59.0-rc.3", "reactxp": "2.0.0", "reactxp-netinfo": "^2.0.0", "reactxp-virtuallistview": "^2.1.0", "reactxp-webview": "^2.0.0", "resub": "^2.0.0-rc.2", "simplerestclients": "^0.2.12", "synctasks": "^0.3.3"

There seems to be some sort of problem when using _buildState and the React lifecycle function componentDidMount in the same component.

When componentDidMount is not present, _buildState runs right after constructor with the initialBuild parameter being true. Then comes a render, and a second _buildState (with initialBuild being false) triggered by a @autoSubscribeWithKey decorator in a data store.

When I add componentDidMount in the component, the second _buildState never gets triggered by the subscription key from the data store.

When I add componentDidMount with a setState call inside it, the _buildState gets called a second time (probably because of the setState call, not because of the subscription key), but its initialBuild parameter remains set to true.

There was no such problem using older versions of ReactXP and ReSub.

Is it not allowed anymore to use _buildState together with componentDidMount in the latest version of ReSub?

berickson1 commented 4 years ago

In your componentDidMount, do you call super.componentDidMount?

deregtd commented 4 years ago

I think you have to use the UNSAFE_ versions of the functions now.

alariej commented 4 years ago

@berickson1 I wasn't calling super in componentDidMount. Now it works, thanks. I should have checked the ToDoList sample app: super.componentDidMount() is correctly called each time.

@deregtd I don't think there is an UNSAFE_ version for componentDidMount. At least not in React 16.12.