react-native-modals icon indicating copy to clipboard operation
react-native-modals copied to clipboard

"Can't call setState..." warning at first dismissal

Open peterh32 opened this issue 6 years ago • 4 comments

Here is what I am finding in 0.16.4. (I do not know about previous versions; this is the only one I've used). React-native 0.57.4.

  • Set dialog visible -> true.
  • Click a link/button in the dialog or click outside the dialog to set prop visible -> false.
  • Result: "Warning: Can't call setState (or forceUpdate) on an unmounted component..."

Only happens the first time you set visible to false.

code:

	<View style={styles.info}>
		<Dialog visible={this.props.visible} onTouchOutside={this.props.hide}>
			<DialogContent>
				<Text style={styles.closeButton} onPress={this.props.hide}>Close</Text>
			</DialogContent>
		</Dialog>
	</View>			

...and I removed all side effects from the hide() method, so it just sets the visible prop.

peterh32 avatar Nov 19 '18 22:11 peterh32

@peterh32 Thanks for report the issue with a detailed information. Just to confirm, even you removed all side effects from hide method still getting that warning right? If so can you show me the code of hide method?

jacklam718 avatar Nov 22 '18 13:11 jacklam718

hide() is a couple of component levels above this one, and it just sets the visible prop to false.

peterh32 avatar Nov 26 '18 23:11 peterh32

Same problem here.

I set visible to false, then navigate to another screen. But the Dialog component does not unmount.

kuzdogan avatar Jul 18 '19 12:07 kuzdogan

Ok found a workaround:

We refer to child component and call handleDismiss() on Dialog.

Set a ReactRef in the constructor:

constructor(props){
    super(props);
    this.dialogRef = React.createRef();
    this.state = {
    ...
    }
  }

Set a reference when rendering the Dialog component:

<Dialog
  visible={this.state.cancelVisible}
  ref={(popupDialog) => { this.dialogRef = popupDialog; }}
> 
</Dialog>

You can dismiss the dialog externally from the parent with:

this.dialogRef.handleDismiss();

kuzdogan avatar Jul 18 '19 12:07 kuzdogan