hello React.js

前端之家收集整理的这篇文章主要介绍了hello React.js前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载地址:

http://www.ruanyifeng.com/blog/2015/03/react.html


  1. /**
  2. * Created by Administrator on 2016-12-8.
  3. */
  4. import React from 'react';
  5. import ReactDOM from 'react-dom';
  6. var names = ['Alice','Emily','Kate'];
  7. /*ReactDOM.render()*/
  8. ReactDOM.render(
  9. <h1>Hello,world!</h1>,document.getElementById('example')
  10. );
  11.  
  12. /*JSX 语法*/
  13. ReactDOM.render(
  14. <div>
  15. {
  16. names.map(function (name) {
  17. return <div>Hello,{name}!</div>
  18. })
  19. }
  20. </div>,document.getElementById('example')
  21. );
  22.  
  23.  
  24. var arr = [
  25. <h1>Hello world!</h1>,<h2>React is awesome</h2>,];
  26. ReactDOM.render(
  27. <div>{arr}</div>,document.getElementById('example')
  28. );
  29.  
  30. /*组件*/
  31. var HelloMessage = React.createClass({
  32. render: function() {
  33. return <h1>Hello {this.props.name}</h1>;
  34. }
  35. });
  36.  
  37. ReactDOM.render(
  38. <HelloMessage name="John" />,document.getElementById('example')
  39. );
  40.  
  41. /*this.props.children*/
  42.  
  43. var NotesList = React.createClass({
  44. render: function() {
  45. return (
  46. <ol>
  47. {
  48. React.Children.map(this.props.children,function (child) {
  49. return <li>{child}</li>;
  50. })
  51. }
  52. </ol>
  53. );
  54. }
  55. });
  56.  
  57. ReactDOM.render(
  58. <NotesList>
  59. <span>hello</span>
  60. <span>world</span>
  61. </NotesList>,document.getElementById('example')
  62. );
  63.  
  64. /*PropTypes*/
  65.  
  66. var MyTitle = React.createClass({
  67. propTypes: {
  68. title: React.PropTypes.string.isrequired,},render: function() {
  69. return <h1> {this.props.title} </h1>;
  70. }
  71. });
  72. var data = "string!";
  73. ReactDOM.render(
  74. <MyTitle title={data} />,document.getElementById('example')
  75. );
  76. /*获取真实的DOM节点*/
  77. var MyComponent = React.createClass({
  78. handleClick: function() {
  79. this.refs.myTextInput.focus();
  80. },render: function() {
  81. return (
  82. <div>
  83. <input type="text" ref="myTextInput" />
  84. <input type="button" value="Focus the text input" onClick={this.handleClick} />
  85. </div>
  86. );
  87. }
  88. });
  89.  
  90. ReactDOM.render(
  91. <MyComponent />,document.getElementById('example')
  92. );
  93.  
  94. /*this.state*/
  95. var LikeButton = React.createClass({
  96. getInitialState: function() {
  97. return {liked: true};
  98. },handleClick: function(event) {
  99. this.setState({liked: !this.state.liked});
  100. },render: function() {
  101. var text = this.state.liked ? 'like' : 'haven\'t liked';
  102. return (
  103. <p onClick={this.handleClick}>
  104. You {text} this. Click to toggle.
  105. </p>
  106. );
  107. }
  108. });
  109.  
  110. ReactDOM.render(
  111. <LikeButton />,document.getElementById('example')
  112. );
  113.  
  114. /*表单*/
  115. var Input = React.createClass({
  116. getInitialState: function() {
  117. return {value: 'Hello!'};
  118. },handleChange: function(event) {
  119. this.setState({value: event.target.value});
  120. },render: function () {
  121. var value = this.state.value;
  122. return (
  123. <div>
  124. <input type="text" value={value} onChange={this.handleChange} />
  125. <p>{value}</p>
  126. </div>
  127. );
  128. }
  129. });
  130.  
  131. ReactDOM.render(<Input/>,document.getElementById('example'));
  132.  
  133. /*组件的生命周期*/
  134. var Hello = React.createClass({
  135. getInitialState: function () {
  136. return {
  137. opacity: 1.0
  138. };
  139. },componentDidMount: function () {
  140. this.timer = setInterval(function () {
  141. var opacity = this.state.opacity;
  142. opacity -= .05;
  143. if (opacity < 0.1) {
  144. opacity = 1.0;
  145. }
  146. this.setState({
  147. opacity: opacity
  148. });
  149. }.bind(this),100);
  150. },render: function () {
  151. return (
  152. <div style={{opacity: this.state.opacity}}>
  153. Hello {this.props.name}
  154. </div>
  155. );
  156. }
  157. });
  158.  
  159. ReactDOM.render(
  160. <Hello name="world"/>,document.getElementById('example')
  161. );
  162.  
  163.  
  164. /*Ajax*/
  165. var UserGist = React.createClass({
  166. getInitialState: function() {
  167. return {
  168. username: '',lastGistUrl: ''
  169. };
  170. },componentDidMount: function() {
  171. $.get(this.props.source,function(result) {
  172. var lastGist = result[0];
  173. if (this.isMounted()) {
  174. this.setState({
  175. username: lastGist.owner.login,lastGistUrl: lastGist.html_url
  176. });
  177. }
  178. }.bind(this));
  179. },render: function() {
  180. return (
  181. <div>
  182. {this.state.username}'s last gist is
  183. <a href={this.state.lastGistUrl}>here</a>.
  184. </div>
  185. );
  186. }
  187. });
  188.  
  189. ReactDOM.render(
  190. <UserGist source="https://api.github.com/users/octocat/gists" />,document.getElementById('example')
  191. );

猜你在找的React相关文章