2017-04-06 38 views
0

嗨我目前正在reactjs,但在非常基本的水平。我試圖解析從父母到孩子以及孩子到父母的數據。父母對孩子正常工作,但是我不能。如何使用reactjs將數據從子項解析到父項?

這裏是整個場景... 我有3個組件..應用程序,家庭和用戶。 從App組件到Home組件我想解析數據。它工作正常。 在家庭組件我有一個輸入字段。如果我寫了一些內容並單擊按鈕,則輸入值將解析爲App組件。應用程序是我的父母和家庭是孩子..

這裏是代碼... APP組件

constructor() { 
      super() 
      this.state = { 
       userName : ' ' 
      }; 
     } 
    changeUName(newName) { 
     this.setState({ 
      userName: newName 
     }); 
     console.log('user',newName); // Getting Undefined 
    } 

    render() { 

    return (

     <div className="App"> 

     <Home changeUser = {() => this.changeUName()} /> 
     </div> 
    ); 
    } 

子組件用戶

constructor(props) { 

     super(); 

     this.state = { 

      userName: '' 

     }; 
    } 
    changeUName(event) { 
     this.setState({ 
      userName: event.target.value 
     }); 
    } 

    nameChange() { 
     console.log(this.state.userName)   // If I write "Test" in input field. I will get the value here. 

     this.props.changeUser(this.state.userName); // Once I parse this value, I am getting undefined.. 
    } 

    render() { 
     return(        
      <div> 
       <h1> HOME Page</h1> 
       Name : <input type="text" value={this.state.userName} onChange={(event) => this.changeUName(event)} ref="uName"/><br/> 
       <button onClick={() => this.nameChange()}>Click</button> 
      </div> 
     ) 
    } 

我不知道它是怎麼了。請指導我。在此先感謝..

+1

的可能的複製[如何傳遞數據從子組件到其在ReactJS中的父代?](http://stackoverflow.com/q uestions/38394015/how-to-pass-data-from-child-component-to-parent-in-reactjs) –

+0

謝謝@Shubham。我看過這篇文章。自昨天以來嘗試了很多,但仍顯示未定義的價值。 –

回答

0

問題是你沒有綁定收到的函數定義的道具。請參見下面的代碼片段

class Parent extends React.Component { 
 
    constructor() { 
 
      super() 
 
      this.state = { 
 
       userName : ' ' 
 
      }; 
 
     } 
 
    changeUName(newName){ 
 
     this.setState({ 
 
      userName: newName 
 
     }); 
 
     console.log('user',newName); // Getting Undefined 
 
    } 
 

 
    render() { 
 

 
    return (
 

 
     <div className="App"> 
 

 
     <Home changeUser = {(value) => this.changeUName(value)} /> 
 
     </div> 
 
    ); 
 
    } 
 
} 
 
class Home extends React.Component { 
 
    constructor(props) { 
 

 
     super(); 
 

 
     this.state = { 
 

 
      userName: '' 
 

 
     }; 
 
    } 
 
    changeUName(event) { 
 
     this.setState({ 
 
      userName: event.target.value 
 
     }); 
 
    } 
 

 
    nameChange() { 
 
     console.log(this.state.userName)   // If I write "Test" in input field. I will get the value here. 
 
     
 
     this.props.changeUser(this.state.userName); 
 
    } 
 

 
    render() { 
 
     return(        
 
      <div> 
 
       <h1> HOME Page</h1> 
 
       Name : <input type="text" value={this.state.userName} onChange={(event) => this.changeUName(event)} ref="uName"/><br/> 
 
       <button onClick={() => this.nameChange()}>Click</button> 
 
      </div> 
 
     ) 
 
    } 
 
    
 
} 
 

 
ReactDOM.render(<Parent/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 
<div id="app"></div>

要不指定用箭頭功能

changeUName = (newName) =>{ 
     this.setState({ 
      userName: newName 
     }); 
     console.log('user',newName); // Getting Undefined 
    } 

的funtion並調用它像

<Home changeUser = {this.changeUName} /> 
+0

謝謝..太棒了。是的,我犯了這個愚蠢的錯誤。 Aagain和再次。再一次感謝你。它現在正在正常工作.. –

0

罪魁禍首是這行代碼:

<Home changeUser = {() => this.changeUName()} /> 

你叫changeUName不帶任何參數。而應該寫:

<Home changeUser = {name => this.changeUName(name)} /> 

或者

<Home changeUser = {this.changeUName} /> 

順便問一下,你有沒有考慮存儲你的用戶名進入終極版商店?如果你還沒有研究過,那麼你一定要研究它。 在這種情況下,您應該將用戶名存儲在redux存儲中,並將其注入到應用程序(以及其他組件)中,方法是將其注入到它們的道具中。

+1

謝謝@Antoine。這是工作。但告訴我一件事,如果我重複使用這些數據,那麼我該怎麼做......?這意味着我想重複使用我的輸入值來進一步處理,例如在另一個子組件中解析或在頁面上打印,那麼我應該怎麼做......? –

+1

感謝它正在工作..我做到了.. –

+0

@ R.Dey歡迎:)關於你的第一個問題,你的意思是你想顯示在其他組件的用戶名?在這種情況下,您可以將userName作爲組件的道具從App傳遞給目標孩子。然而,在這種情況下,我會強烈建議你在連接時使用redux,所以你不必實際轉發userName:你可以直接'連接'目標子組件。 –

相關問題