Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. class ReactNativeComponent extends React.Component {
  2. constructor(props) {
  3. super(props)
  4. this.state = {
  5. clicks: 0
  6. };
  7. }
  8.  
  9. componentDidMount() {
  10. AppState.addEventListener('change', this.incrementClicks);
  11. }
  12.  
  13. componentWillUnmount() {
  14. AppState.removeEventListener('change', this.incrementClicks);
  15. }
  16.  
  17. // контекст со стрелочкой берется из окружения, а оно - текущий объект класса
  18. incrementClicks = () => {
  19. this.setState(prevState => {
  20. return {
  21. clicks: this.state.clicks + 1
  22. }
  23. });
  24. }
  25.  
  26. render() {
  27. return (
  28. <View>
  29. <Text>{`My Component ${this.state.clicks} clicks`}</Text>
  30. <Text>this.props.headerText</Text>
  31. <TouchableHighlight
  32. underlayColor={colors.buttonsOverlayColor}
  33. onPress={this.incrementClicks}
  34. style={
  35. {
  36. width: 100,
  37. height: 100,
  38. backgroundColor:'red'
  39. }
  40. }/>
  41.  
  42. </View>
  43. );
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement