Advertisement
asimryu

REACT TEST 1

Oct 18th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>react - 1</title>
  6. <script src="https://unpkg.com/react@15.3.2/dist/react.js"></script>
  7. <script src="https://unpkg.com/react-dom@15.3.2/dist/react-dom.js"></script>
  8. <script src="https://unpkg.com/babel-core@5.8.38/browser.min.js"></script>
  9. </head>
  10. <body>
  11. <div id="root"></div>
  12. <script type="text/babel">
  13. var App = React.createClass({
  14. getInitialState: function(){
  15. return {count: 0};
  16. },
  17. buy: function(){
  18. this.setState({count: this.state.count + 1});
  19. this.props.handleTotal(this.props.price);
  20. },
  21. show: function(){
  22. alert(this.props.name);
  23. },
  24. render: function(){
  25. return (
  26. <div>
  27. <p>{this.props.name} - ${this.props.price}</p>
  28. <button onClick={this.buy}>구매</button>
  29. <button onClick={this.show}>보기</button>
  30. <h3>수량 : {this.state.count} 개</h3>
  31. <hr/>
  32. </div>
  33. );
  34. }
  35. });
  36.  
  37. var Total = React.createClass({
  38. render: function(){
  39. return (<div><h3>Total: ${this.props.total}</h3></div>);
  40. }
  41. });
  42.  
  43. var Phones = React.createClass({
  44. getInitialState: function(){
  45. return {
  46. total: 0,
  47. phonelist: [
  48. {name: "Android",price: 199},
  49. {name: "Apple",price: 220},
  50. {name: "Nokia",price: 80},
  51. {name: "WindowsPhone",price: 50}
  52. ]
  53. };
  54. },
  55. getTotal: function(price){
  56. this.setState({total: this.state.total + price});
  57. },
  58. render: function(){
  59. var temp = this;
  60. var products = this.state.phonelist.map(function(product,i){
  61. return (<App name={product.name} price={product.price} key={i} handleTotal={temp.getTotal}/>);
  62. });
  63. return (
  64. <div>
  65. {products}
  66. <Total total={this.state.total}/>
  67. </div>
  68. );
  69. }
  70. });
  71.  
  72. ReactDOM.render(<Phones/>,document.getElementById("root"));
  73. </script>
  74. </body>
  75. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement