Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modal not hiding when setState updates are done in try / catch and the response is quick #28753

Closed
lucastonon opened this issue Apr 25, 2020 · 4 comments
Labels
Component: Modal Needs: Author Feedback Needs: Environment Info Please run `react-native info` and edit your issue with that command's output. Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@lucastonon
Copy link

lucastonon commented Apr 25, 2020

Hi, I would like to reopen #10417

I am having this problem for a while and the only solution that worked for me was to create a timeout big enough that even in slow devices it works.

When using try/catch, if the work is finished quickly (quick api responses for instance), when we setState({showLoader: false}) it won't remove the Modal and we will have to close and run the app again to clear it.

I am going to use the same example as the issue link mentioned

makeRequest = () => {
  this.setState({ loading: true })
  Media.request(url)
  .then(response => {
    // todo
  }, e => {
    this.setState({ loading: false })
  })
}

render() {
  return(
    <View>
      <Modal
          animationType={'slide'}
          transparent={false}
          visible={this.state.loading}
          onRequestClose={() => {} }
       >
        <View></View>
      </Modal>
      <Text onPress={this.makeRequest}>Do Magic</Text>
    </View>
  )
}

e7a3ffba-9406-11e6-9731-bfbe503324b2

@react-native-bot react-native-bot added Component: Modal Needs: Author Feedback Needs: Environment Info Please run `react-native info` and edit your issue with that command's output. and removed Needs: Triage 🔍 labels Apr 25, 2020
@github-actions
Copy link

⚠️ Missing Environment Information
ℹ️ Your issue may be missing information about your development environment. You can obtain the missing information by running react-native info in a console.

@RinorDreshaj
Copy link

have you tried putting the request after the setState

makeRequest = () => {
	this.setState({ 
		loading: true 
	}, () => {
		// this ensures that the state has been set to loading: true
		Media.request(url)
		.then(response => {
			// todo
		}, e => {
			this.setState({ loading: false })
		})	
	})
}

and to make the code cleaner you can use .catch clause as its the same as onRejected callback: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch

and even better use the finally clause to set the loading to false, so you know that either if promise is fulfilled or rejected the laoding will be set to false

makeRequest = () => {
	this.setState({ 
		loading: true 
	}, () => {
		// this ensures that the state has been set to loading: true
		Media.request(url)
		.then(response => {/* todo */})
		.catch(error => {})
		.finally(() => {
			this.setState({ loading: false })
		})		
	})
}

@stale
Copy link

stale bot commented Jul 29, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jul 29, 2020
@stale
Copy link

stale bot commented Aug 8, 2020

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

@stale stale bot closed this as completed Aug 8, 2020
@facebook facebook locked as resolved and limited conversation to collaborators Oct 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Component: Modal Needs: Author Feedback Needs: Environment Info Please run `react-native info` and edit your issue with that command's output. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

3 participants