Guest User

Untitled

a guest
Nov 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. //PARENT
  2.  
  3. import React from 'react';
  4. import ReactDOM from 'react-dom';
  5. import { Child } from './Child';
  6. import { Sibling } from './Sibling';
  7.  
  8. class Parent extends React.Component {
  9. constructor(props) {
  10. super(props);
  11.  
  12. this.state = { name: 'Frarthur' };
  13.  
  14. this.changeName = this.changeName.bind(this);
  15. }
  16.  
  17. changeName(newName) {
  18. this.setState({
  19. name: newName
  20. });
  21. }
  22.  
  23. render() {
  24. return (
  25. <div>
  26. <Child onChange={this.changeName} />
  27. <Sibling name={this.state.name} />
  28. </div>
  29. );
  30. }
  31. });
  32.  
  33. ReactDOM.render(
  34. <Parent />,
  35. document.getElementById('app')
  36. );
  37.  
  38.  
  39. //CHILD
  40.  
  41. import React from 'react';
  42.  
  43. export class Child extends React.Component {
  44. constructor(props) {
  45. super(props);
  46.  
  47. this.handleChange = this.handleChange.bind(this);
  48. }
  49.  
  50. handleChange(e) {
  51. const name = e.target.value;
  52. this.props.onChange(name);
  53. }
  54.  
  55. render() {
  56. return (
  57. <div>
  58. <select
  59. id="great-names"
  60. onChange={this.handleChange}>
  61.  
  62. <option value="Frarthur">Frarthur</option>
  63. <option value="Gromulus">Gromulus</option>
  64. <option value="Thinkpiece">Thinkpiece</option>
  65. </select>
  66. </div>
  67. );
  68. }
  69. }
  70.  
  71. //SIBLING
  72.  
  73. import React from 'react';
  74.  
  75. export class Sibling extends React.Component {
  76. render() {
  77. const name = this.props.name;
  78. return (
  79. <div>
  80. <h1>Hey, my name is {name}!</h1>
  81. <h2>Don't you think {name} is the prettiest name ever?</h2>
  82. <h2>Sure am glad that my parents picked {name}!</h2>
  83. </div>
  84. );
  85. }
  86. }
Add Comment
Please, Sign In to add comment