Skip to content

Instantly share code, notes, and snippets.

@nhancv
Created February 23, 2018 11:12
Show Gist options
  • Save nhancv/fd4009e26e6eeed24fdcb5e55f006dee to your computer and use it in GitHub Desktop.
Save nhancv/fd4009e26e6eeed24fdcb5e55f006dee to your computer and use it in GitHub Desktop.
Throttle press action
import React, { Component } from 'react'
import { Dimensions, ScrollView, Text, Image, View, StyleSheet, TouchableOpacity } from 'react-native'
import { Container, Header, Left, Body, Right, Button, Icon, Title, Content } from 'native-base'
import styles from './Main.Styles'
export default class MainScreen extends Component {
constructor(props) {
super(props)
this.state = {
btn1: 0,
btn2: 0,
log: ''
}
this.onPressDelay = _.throttle(this.onPressAction, 2000, { 'trailing': false })
}
onPressAction = (func) => {
func()
}
render() {
return (
<Container>
<Header>
<Body>
<Title>MainScreen</Title>
</Body>
</Header>
<View style={[styles.container, { flexDirection: 'column' }]}>
<Text>{this.state.log}</Text>
<View style={styles.container}>
<View style={styles.center}>
<Text>{this.state.btn1}</Text>
<View><Button onPress={() => {
this.onPressDelay(() => {
this.setState({
btn1: this.state.btn1 + 1,
log: 'setState1: ' + this.state.btn1
})
})
}} bordered warning style={styles.button}>
<Text>Button 1</Text>
</Button></View>
</View>
<View style={styles.center}>
<Text>{this.state.btn2}</Text>
<View><Button onPress={() => {
this.onPressDelay(() => {
this.setState({
btn2: this.state.btn2 + 1,
log: 'setState2: ' + this.state.btn2
})
})
}} bordered danger style={styles.button}>
<Text>Button 2</Text>
</Button></View>
</View>
</View>
</View>
</Container >
)
}
}
import { StyleSheet } from 'react-native'
export default StyleSheet.create({
container: {
flexDirection: 'row',
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
center: {
alignItems: 'center',
justifyContent: 'center',
borderColor: 'red',
borderWidth: 1,
margin: 2,
padding: 20,
flex: 1
},
button: {
marginTop: 10,
paddingLeft: 5,
paddingRight: 5
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment