Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. {/*I'm trying to pass a function two levels deep and it doesnt seem to be working (this is in react.js)*/}
  2. {/*note these are snippets of the code to give an idea - below wont compile as is*/}
  3.  
  4. {/* ++++++++++++++++++++Grandparent function++++++++++++++++++++*/}
  5.  
  6. handleLogIn(username, password){
  7. let setusername = this.state.username;
  8. let setpassword = this.state.password;
  9.  
  10. {/*call to the backend and bcrypt goes here*/}
  11.  
  12. this.setState({
  13. username:setusername,
  14. password:setpassword
  15. });
  16.  
  17. let updatedusername = this.state.username;
  18. let updatedpassword = this.state.password;
  19.  
  20.  
  21. console.log('username: ', updatedusername, ' password: ', updatedpassword);
  22. }
  23.  
  24.  
  25.  
  26. render() {
  27.  
  28. const xProfilePagex = () => {
  29. return(
  30. <div>
  31. <ProfilePage handleLogIn={this.handleLogIn.bind(this)}/>
  32.  
  33. {/*++++++++++++++++++++Parent function++++++++++++++++++++*/}
  34.  
  35. this.state = {
  36. handleLogIn: this.props.handleLogIn,
  37. username: this.props.username,
  38. password: this.props.password
  39. }
  40. }
  41.  
  42. render() {
  43. return (
  44. <div>
  45. <h1>ProfilePage</h1>
  46. <LogIn handleLogIn={this.state.handleLogIn.bind(this)}/>
  47.  
  48. {/*++++++++++++++++++++Child function++++++++++++++++++++*/}
  49.  
  50.  
  51. constructor(props){
  52. super(props);
  53. this.state = {
  54. username: this.props.username,
  55. password: this.props.password
  56. }
  57. }
  58.  
  59.  
  60. handleLogIn(e){
  61. e.preventDefault();
  62. this.props.handleLogIn(this.state.username, this.state.password);
  63.  
  64. console.log('inside login. username: ', this.state.username, ' password: ', this.state.password);
  65.  
  66. }
  67.  
  68.  
  69. render() {
  70. return (
  71. <div>
  72. <p>LogIn</p>
  73. <form name="log-in" id="log-in">
  74.  
  75. <input
  76. onChange={(e)=>this.setState({username: e.target.value })}
  77. type="text"
  78. name="question"
  79. id="question"
  80. placeholder="username"/>
  81.  
  82. <input
  83. onChange={(e)=>this.setState({password: e.target.value })}
  84. type="text"
  85. name="question"
  86. id="question"
  87. placeholder="password"/>
  88.  
  89. <button onClick={(e)=>this.handleLogIn(e)}>Log In</button>
  90.  
  91. </form>
  92.  
  93.  
  94. {/* I can pass the form to the console log in the child function, but it doesn't bubble up to the grandparent. Please help.*/}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement