Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import { render } from 'react-dom';
  3.  
  4. class TopBox extends Component {
  5.  
  6. render () {
  7. const s = {backgroundColor:'red'};
  8. const all_names = this.props.all_names.map(n => {
  9. return (
  10. <p>{n}</p>
  11. );
  12. });
  13. return (
  14. <div style={s}>
  15. {all_names}
  16. </div>
  17. );
  18. }
  19.  
  20. }
  21.  
  22.  
  23. class BottomBox extends Component {
  24.  
  25. constructor() {
  26. super();
  27. this.handler = this.handler.bind(this);
  28. }
  29.  
  30. handler() {
  31. this.props.some_function(''+Math.random());
  32. }
  33.  
  34. render () {
  35. const s = {backgroundColor:'orange'};
  36. return (
  37. <div style={s}
  38. onClick={this.handler}
  39. >
  40. Click me
  41. </div>
  42. );
  43. }
  44.  
  45. }
  46.  
  47. class Application extends Component {
  48.  
  49.  
  50. constructor() {
  51. super();
  52. this.state = {all_names: ['hello']};
  53. this.add_name = this.add_name.bind(this);
  54. }
  55.  
  56. add_name(item) {
  57. this.setState({
  58. all_names:[...this.state.all_names, item]
  59. });
  60. }
  61.  
  62. render() {
  63. return (
  64. <div>
  65. <TopBox
  66. all_names={this.state.all_names}
  67. />
  68. <BottomBox
  69. some_function={this.add_name}
  70. />
  71. </div>
  72. );
  73. }
  74. };
  75.  
  76.  
  77.  
  78. render(<Application/>,
  79. document.getElementById('container'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement