2016-11-24 90 views
1

我有這樣使用自定義功能反應

class Hourly extends Component { 
    render() { 
    return (
     <div key={this.props.key} id={this.props.id} className="col-xs-3 text-center vcenter box parent"> 
     <div className="child"> 
      <div>{this._theDay(this.props.day)}</div> 
      <div>{this.props.summary}</div> 
     </div> 
     </div> 
    ); 
    } 
} 

我想我在另一個組件後使用組件使用自定義函數(_theDay)組件

自定義功能:

_theDay(time) { 
    moment.locale('en-gb'); 
    var dateTime = moment(time*1000).format('dddd'); 
    return dateTime; 
    } 

我調用組件後來與

 <Hourly 
      key={hour.time} 
      id={hour.time} 
      day={hour.time} 
      summary={hour.summary} 
     /> 

但是我得到的錯誤:Uncaught (in promise) DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.(…)

當我只是用<div>{this.props.day}</div>;一切工作正常,但後來我不能使用我的自定義功能

+0

試圖重現您的問題,似乎爲我工作: -/http://jsbin.com/wuqupehaqa/edit?html,js,output – BFree

+0

嗯我可以看到, ...我嘗試重新組織我的代碼,就像在你的例子中,現在告訴我'_this4._theDay不是一個函數' – frisk0

+1

對,所以我把'_theDay'函數放在Hourly類中,我猜測'因爲你需要在其他地方重新使用該功能,所以不適合你。如果是這樣的話,調用它時不要使用'this._Day',並將函數放置在兩個組件都可以訪問的地方。 – BFree

回答

0

我覺得這事做陣營試圖更新DOM道具改變後。嘗試使用與props.day相關的<div/>上的鑰匙來幫助它知道孩子已經改變。

class Comp extends React.Component { 
    render() { 
    let day = this._theDay(this.props.day) 
    return <div key={day}>{day}</div>; 
    } 
} 

這裏的documentation on reconciling the DOM.

+0

爲了簡單起見,我剝去了組件,這可能是一個壞主意。我已經有了一把鑰匙。我剛剛更新了我的問題 – frisk0