Skip to content

Instantly share code, notes, and snippets.

@stigi
Created August 5, 2019 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stigi/8d0cc6a227e509e0c7266a8dba64e713 to your computer and use it in GitHub Desktop.
Save stigi/8d0cc6a227e509e0c7266a8dba64e713 to your computer and use it in GitHub Desktop.
import React from 'react'
import { Button } from 'react-native'
interface Props {}
interface State {
isDoing: boolean
}
export class WithButton extends React.Component<Props, State> {
public state: State = { isDoing: false }
private handlePress = () => {
this.setState(prev => {
if (prev.isDoing) {
// async action already running
return null
}
this.doIt().finally(() => this.setState({ isDoing: false }))
return { isDoing: true }
})
}
private doIt = async () => {
return new Promise(res => setTimeout(res, 3000))
}
public render() {
return <Button disabled={this.state.isDoing} onPress={this.handlePress} title={`Hit me`} />
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment