我有这个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