2014-11-05 10 views
8

वचन में उपयोग किए गए सेटस्टेट के बाद पुन: प्रस्तुत नहीं करता है हर बार प्रोप बदल जाते हैं, घटक टर्म चेंज पर कॉल करेगा और इस घटक के लिए विवरण प्राप्त करेगा जो ऑब्जेक्ट्स की एक सरणी देता है।प्रतिक्रिया

समस्या यह है कि जब सेटस्टेट कहा जाता है, कुछ भी नहीं होता है और घटक को ताजा विवरण के साथ पुन: प्रस्तुत नहीं किया जाता है।

module.exports = React.createClass({ 
displayName: 'TaxonomySelect', 

getInitialState: function() { 
    return { 
     children: undefined 
    }; 
}, 

componentDidMount: function() { 
    this.onTermChange(this.props.term); 
}, 

componentWillReceiveProps: function (nextProps) { 
    this.props.term = this.props.term || null; 

    if (this.props.term) { 
     this.onTermChange(nextProps.term.id); 
    } else { 
     this.onTermChange(nextProps.term); 
    } 
}, 

onTermChange: function (term) { 
    this.setState({children: undefined}); 

    TaxonomyStore.getTerm(this.props.term) 
     .bind(this) 
     .then(function (term) { 
      TaxonomyStore.merge(9999,{ 
        description: 'No specific topic', 
        id: 9999 
      }); 
      if (term.path && term.path.length === 3) { 
       term.children.unshift(TaxonomyStore.get(9999)); 
      } 

      console.log(term.id, term.children); 

      this.setState({children: term.children}); 
      this.forceUpdate(); 
      this.render(); 
     }); 
}, 

onSelect: function (id) { 
    if (this.props.onChange) { 
     this.props.onChange(TaxonomyStore.get(id)); 
    } 
}, 

render: function() { 
    if (!Array.isArray(this.state.children) || this.state.children.length < 1) { 
     return null; 
    }; 

    var options = this.state.children.map(function (term) { 
     return { 
      value: term.id.toString(), 
      label: term.description 
     }; 
    }); 

    var value = this.props.value && this.props.value.toString(); 

    return (
     <div> 
      <Select name={this.props.name} value={value} options={options} onChange={this.onSelect} /> 
     </div> 
    ); 
} 
}); 

उत्तर

1

आप this.forceUpdate() कॉल करने के लिए यदि आप this.setState बुला रहे हैं की जरूरत नहीं होनी चाहिए। साथ ही, आपको किसी घटक की render विधि कभी भी कॉल नहीं करनी चाहिए।

यह कहना मुश्किल है कि यह फिर से प्रस्तुत क्यों नहीं कर रहा है, लेकिन मैं कह रहा हूं कि कुछ debugger कथन होगा। मुझे लगता है कि में हमेशा this.onTermChange पर कॉल करना एक संभावित समस्या हो सकती है।

2

जब आप this.setState({children: term.children});this पर कॉल करते हैं तो उस फ़ंक्शन के बराबर होता है, जिसे प्रतिक्रिया घटक में परिभाषित किया गया था।

शायद एक अपवाद होता है, लेकिन आपका वादा .error को जाल और लॉग इन करने के लिए कॉल नहीं करता है।

0

मैं एक ही समस्या में आया, जो @ z5h से प्रेरित है, मैं Promise के बाहर संदर्भित करने के लिए स्थानीय विचित्र का उपयोग करता हूं, और यह काम करता है!

आपके मामले में:

this के बारे में
onTermChange: function (term) { 
    this.setState({children: undefined}); 

    let _this = this; // change 
    TaxonomyStore.getTerm(this.props.term) 
     .bind(this) 
     .then(function (term) { 
      TaxonomyStore.merge(9999,{ 
        description: 'No specific topic', 
        id: 9999 
      }); 
      if (term.path && term.path.length === 3) { 
       term.children.unshift(TaxonomyStore.get(9999)); 
      } 

      console.log(term.id, term.children); 

      _this.setState({children: term.children}); //change 

     }); 
} 

अधिक js में: How does the “this” keyword work?