React sınıflarımı ES6'ya dönüştürmeye çalışıyorum, ancak bu işlemde zorluk yaşıyorum. görünüm oluştur.Kurucuda bağlanma, parametrelere nasıl geçilir
Şimdi, bir parametreyi gereken bir setstate olan, örneğin bir kök modülü varsa: customElement modülü içinde Sonra
<customElement updateMood={this.updateMood}></customElement>
:
constructor() {
super();
this.state = {
mood: ""
};
this.updateMood(value) = this.updateMood.bind(this,value);
}
updateMood(value) {
this.setState({mood: value});
}
Sonra bir bileşene bu işlevi geçmesi
constructor() {
super();
}
update(e) {
this.props.updateMood(e.target.value);
}
ve
işlemek: Ben böyle bir şey var<input onChange={this.update} />
Bu doğru yol mu? Devam etsin diye ;-(alamayan bu yana sözdizimi hatası olduğu için
Sadece this.updateMood (value) = this.updateMood.bind öğesini (this, value); ve değiştirin. customElement> 'Not bunu sahne verirken bağla. –
Vishwanath
* "Bu doğru yol mu?" * Çalışmıyorsa muhtemelen değil: P İhtiyacınız olan tek şey this.updateMood = this.updateMood.bind (this); '. Nasıl çalıştığını daha iyi anlamak için '.bind' ve genel olarak işlevlerle ilgili belgeleri okumalısınız. Bunun React veya ES6 btw ile ilgisi yoktur. Bu kapsamlı cevap için bir milyon teşekkür –