2017-06-03 27 views
1

誰知道我的組件中的變更道具如何使用引用來響應js?如何在React js中設置道具使用引用js

<MyComponent 
    ref={'input1'} 
    name={'input1'} 
    label={interestsName} 
    disabled={ false} 
    onChange={this.myFunction}/> 

的onChange後,我打電話與

myFunction =()=>{console.log(this.rews[input1].props.disable);} 

我可能會改變使用道具沒有裁判使用狀態的功能?因爲我有許多'15'組件,比如這個組件。謝謝。

+0

道具只是一個函數調用的參數進行管理,你不能改變他們,至少是一個不好的模式 –

回答

0

你不應該在這種情況下使用的裁判,你應該使用狀態來改變你的孩子的道具:

class MainComponent extends React.Component { 
    constructor(props) { 
    super(props) 
    this.state = { 
     disable: false, 
    }; 
    this.onChange = this.onChange.bind(this); 
    } 

    onChange() { 
    this.setState({ disable: true }); 
    } 

    <MyComponent 
    name="input1" 
    label={interestsName} 
    disabled={this.state.disable} 
    onChange={this.onChange} 
    /> 
} 

<MyComponent>使用componentWillReceiveProps()檢測新道具的價值。

+0

我使用這個組件的數組,如果我可以使用狀態,我更改數組的所有組件單擊。 – Trik

+0

對不起,我不明白你的問題? –

1

您不能更改子類的道具,更多請參考link

爲了您的功能使用,您可以使用狀態更改change事件的值。還有一件事你應該保持改變組件屬性的邏輯應該保留在組件內。這將有助於我們爲不同的組件維護不同的狀態。

class MyComponent extends React.Component { 
    constructor(props) { 
    this.state = { 
    disable: props.disabled 
    }; 
    } 

    myFunction() { 
    console.log(this.state); 
    } 
} 

可以遍歷上述成分,它可用於15次不同的狀態可以爲每一個元素