Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. Hello World
  2. ===
  3. ```
  4. ReactDOM.render(
  5. React.createElement('h1', {}, 'Hello World'),
  6. document.getElementById('container')
  7. );
  8. ```
  9.  
  10. Intro
  11. ===
  12.  
  13. ```
  14. const currentTime = (new Date).toLocaleTimeString();
  15.  
  16. ReactDOM.render(
  17. React.createElement('h1', {}, currentTime),
  18. document.getElementById('container')
  19. );
  20. ```
  21.  
  22. Make ClockFace component
  23. ===
  24. ```
  25. class ClockFace extends React.Component {
  26. const currentTime = (new Date).toLocaleTimeString();
  27.  
  28. render() {
  29. return React.createElement('h1', {}, currentTime)
  30. }
  31. }
  32.  
  33. const currentTime = (new Date).toLocaleTimeString();
  34.  
  35. ReactDOM.render(
  36. React.createElement(ClockFace, { time: currentTime }),
  37. document.getElementById('container')
  38. );
  39. ```
  40.  
  41. Make ticking clock (higher order component)
  42. ===
  43. ```
  44. class ClockFace extends React.Component {
  45. render() {
  46. return React.createElement('h1', {}, this.props.time);
  47. }
  48. }
  49.  
  50. function Timekeeper(WrappedComponent) {
  51. return class Timekeeper extends React.Component {
  52. render() {
  53. return React.createElement(WrappedComponent, { time: 'Starting...' });
  54. }
  55. }
  56. }
  57.  
  58. function TimekeeperHoc(WrappedComponent) {
  59. return class Timekeeper extends React.Component {
  60. constructor() {
  61. super();
  62.  
  63. this.state = { time: 'Starting...' };
  64. setInterval(this.updateTime.bind(this), 1000);
  65. }
  66.  
  67. updateTime() {
  68. this.setState({ time: (new Date).toLocaleTimeString() })
  69. }
  70.  
  71. render() {
  72. const props = Object.assign({}, { time: this.state.time }, this.props);
  73.  
  74. return React.createElement(WrappedComponent, props)
  75. }
  76. }
  77. }
  78.  
  79. const Clock = TimekeeperHoc(ClockFace);
  80.  
  81. ReactDOM.render(
  82. React.createElement(Clock),
  83. document.getElementById('container')
  84. );
  85. ```
  86. JSX
  87. ===
  88. ```
  89. ReactDOM.render(
  90. <Clock />,
  91. document.getElementById('container')
  92. );
  93.  
  94. class ClockFace extends React.Component {
  95. render() {
  96. <h1>{this.props.time}</h1>
  97. }
  98. }
  99.  
  100. function TimekeeperHoc(WrappedComponent) {
  101. return class Timekeeper extends React.Component {
  102. constructor() {
  103. super();
  104.  
  105. this.state = { time: 'Starting...' };
  106. setInterval(this.updateTime.bind(this), 1000);
  107. }
  108.  
  109. updateTime() {
  110. this.setState({ time: (new Date).toLocaleTimeString() })
  111. }
  112.  
  113. render() {
  114. return <WrappedComponent {...this.props} time={this.state.time} />
  115. }
  116. }
  117. }
  118. ```
  119. Stateless Function
  120. ===
  121. ```
  122. function ClockFace(props) {
  123. return (
  124. <h1 style={{backgroundColor: props.colour}}>{props.time}</h1>
  125. )
  126. }
  127. ```
  128. ES6 Stateless
  129. ===
  130. ```
  131. const ClockFace = ({time, colour}) => <h1 style={{backgroundColor}}>{time}</h1>
  132. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement