我有這個strage情況下,也許我失蹤如何反應componentDidMount
的作品,也許這是一個奇怪的行爲(右知道我不能提供一個真正的小提琴與jQuery插件的東西,但我敢肯定的問題比插頭本身更寬)。爲什麼componentDidUpdate看不到新插入的dom節點?
var Hello = React.createClass({
getInitialState() {
return {content: '<div id="target">ciao!</div>'}
},
componentDidMount() {
const target = $('#target').init();
this.target = target;
},
componentWillReceiveProps(nextProps) {
if (condition) {
this.target.destroy(); // this remove even the DOM node
}
},
componentDidUpdate() {
const target = $('#target').init(); // HERE PROBLEM! can't find the #target element
this.target = target;
}
render: function() {
return <div>Hello {this.state.content}</div>;
}
});
我不明白爲什麼後'condition'是真實和DOM節點都通過插件破壞,componentDidUpdate
沒有找到#target
,在我的腦海裏應該是「重新渲染」在componentWillReceiveProps
之後的第一個渲染中。我希望在那裏,因爲它從最初的狀態讀取它永遠不會改變。
我錯過了什麼?
你不能呈現原始的HTML一樣,不使用恰當地命名爲['dangerouslySetInnerHtml'(https://facebook.github.io/react/docs/dom-elements.html#dangerouslysetinnerhtml)。 –
@JoeClay並非如此。無論如何,我試過,不改變任何東西.. – ciaoben