Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. class ReactNativeComponent extends React.Component {
  12. constructor(props) {
  13. super(props)
  14. this.state = {
  15. clicks: 0
  16. };
  17. }
  18.  
  19. componentDidMount() {
  20. AppState.addEventListener('change', this.incrementClicks);
  21. }
  22.  
  23. componentWillUnmount() {
  24. AppState.removeEventListener('change', this.incrementClicks);
  25. }
  26.  
  27. // контекст со стрелочкой берется из окружения, а оно - текущий объект класса
  28. incrementClicks = () => {
  29. this.setState(prevState => {
  30. return {
  31. clicks: this.state.clicks + 1
  32. }
  33. });
  34. }
  35.  
  36. render() {
  37. return (
  38. <View>
  39. <Text>`My Component ${this.state.clicks} clicks`</Text>
  40. <Text>this.props.headerText</Text>
  41. <TouchableHighlight
  42. underlayColor={colors.buttonsOverlayColor}
  43. onPress={this.incrementClicks}
  44. style={
  45. {
  46. width: 100,
  47. height: 100,
  48. backgroundColor:'red'
  49. }
  50. }>
  51. </TouchableHighlight>
  52. </View>
  53. );
  54. }
  55. }
  56. </script>
  57.  
  58.  
  59.  
  60. <script id="jsbin-source-javascript" type="text/javascript">class ReactNativeComponent extends React.Component {
  61. constructor(props) {
  62. super(props)
  63. this.state = {
  64. clicks: 0
  65. };
  66. }
  67.  
  68. componentDidMount() {
  69. AppState.addEventListener('change', this.incrementClicks);
  70. }
  71.  
  72. componentWillUnmount() {
  73. AppState.removeEventListener('change', this.incrementClicks);
  74. }
  75.  
  76. // контекст со стрелочкой берется из окружения, а оно - текущий объект класса
  77. incrementClicks = () => {
  78. this.setState(prevState => {
  79. return {
  80. clicks: this.state.clicks + 1
  81. }
  82. });
  83. }
  84.  
  85. render() {
  86. return (
  87. <View>
  88. <Text>`My Component ${this.state.clicks} clicks`</Text>
  89. <Text>this.props.headerText</Text>
  90. <TouchableHighlight
  91. underlayColor={colors.buttonsOverlayColor}
  92. onPress={this.incrementClicks}
  93. style={
  94. {
  95. width: 100,
  96. height: 100,
  97. backgroundColor:'red'
  98. }
  99. }>
  100. </TouchableHighlight>
  101. </View>
  102. );
  103. }
  104. }
  105. </script></body>
  106. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement