2014-11-06 8 views
13

मेरे पास यह घटक है। मैं प्रत्येक सूची में कॉल कॉलर को पास करना चाहता हूं। यदि मैं इसे नीचे की तरह करता हूं, bind(this) के साथ, यह ठीक से काम करता है। bind(): You are binding a component method to the component. React does this for you automatically in a high-performance way, so you can safely remove this call.प्रतिक्रिया के लिए मुझे घटक विधि को बाध्य करने के खिलाफ चेतावनी क्यों देता है?

var MyList = React.createClass({ 
    render: function() { 
    var listElements = this.props.myListValues.map(function (val) { 
     return (
     <ListElement onCallHandler={this.props.parentsCallHandler} val={val} /> 
     ); 
    }.bind(this)); 
    return (
     <ul> 
      {listElements} 
     </ul> 
    ); 
    } 
}); 

मैं इसे बाँध नहीं है, तो अपने बच्चों को कॉल हैंडलर के बारे में पता नहीं है: समस्या यह है कि मैं इस चेतावनी कंसोल में प्रतिक्रिया से प्राप्त है। मैं अलग-अलग क्या कर सकता था?

पुनश्च:

मैं destructuring कार्य के बारे में पता है, की तरह समझाया http://facebook.github.io/react/docs/transferring-props.html#transferring-with-...-in-jsx, लेकिन मैं सद्भाव उपयोग करने के लिए नहीं करना चाहती।

+0

द्वारा "कॉल हैंडलर" आप ईवेंट हैंडलर मतलब है? घटनाओं को माता-पिता को बुलबुला करना चाहिए और वहां पर कब्जा कर लिया जा सकता है। मैं क्या खो रहा हूँ? – Mathletics

+1

मुझे वह त्रुटि नहीं दिखाई दे रही है: http://jsfiddle.net/s6dok0xv। –

उत्तर

29

त्रुटि कोड में कहीं और से आ रही है। जब आप this.someFunction.bind(something) करते हैं तो आपको त्रुटि मिलती है और कुछ null नहीं है।

this.someFunction.bind({}, foo); // warning 
this.someFunction.bind(this, foo); // warning, you're doing this 
this.someFunction.bind(null, foo); // okay! 

अपने कोड में .bind(this के लिए एक खोज करें हमलावर लाइन खोजने के लिए।

+1

ओह, आप सही हैं, यह कहीं और था। मैं अब शर्मिंदा हूं – andersem

+3

@FakeRainBrigand ऐसा लगता है कि अब यह दूसरे मामले में चेतावनी नहीं देता है,'बिंद (यह, foo) '। यह '.bind (यह)' के साथ करता है। प्रतिक्रिया @0.13.1 के साथ परीक्षण किया। https://jsfiddle.net/69z2wepo/17453/ और दस्तावेज़ वर्तमान में यह दिखाते हुए दिखाते हैं: https://facebook.github.io/react/tips/communicate-between-components.html। जब बाइंड को बांधना है और जब प्रतिक्रिया में नहीं है तो यह कहानी शायद अत्यधिक जटिल लगती है। – JMM

+2

कारण।'बिंद (शून्य, 'काम करता है, क्योंकि प्रतिक्रिया के बाद से autobinds [v0.4] (http://facebook.github.io/react/blog/2013/07/02/react-v0-4 -autobind-by-default.html), '.bind (null' करके, हम essentiall निष्पादित कर रहे हैं' this.methond.bind (this) .bind (null, arg1, arg2) '। बस सोचा कि आप में से कुछ चाहें जानने के लिए ** क्यों ** यह काम किया। –

4

यहाँ अद्यतन किया जाता है डॉक्स उदाहरण

React.createClass({ 
    onClick: function(event) {/* do something with this */}, 
    render: function() { 
    return <button onClick={this.onClick} />; 
    } 
}); 

Update in ReactJS Docs

संबंधित मुद्दे

 संबंधित मुद्दे