[英]React with jquery does not update the UI on dom change
I have a slider that i am adding slides to it when the user clicks on a button with setState and then call the jquery plugin to update, i see the new element on the dom but its seems like the jquery slider does not update the change and add the slider. 我有一个滑块,当用户单击带有setState的按钮然后调用jquery插件进行更新时,我正在向其中添加幻灯片,我在dom上看到了新元素,但似乎jquery滑块不会更新更改,并且添加滑块。 what is the problem?
问题是什么?
var Posts = React.createClass({
componentDidMount: function() {
swiper = new Swiper ('.swiper-container', {
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev'
});
},
updateData: function() {
this.setState({
posts: [{title:Math.random()}, {title:Math.random()},{title:Math.random()}]
});
swiper.update(true);
},
getInitialState: function() {
return {
posts: [{title:1111}, {title:2222}]
}
},
render: function() {
console.log('run');
var postNodes = this.state.posts.map(function( post, i ) {
return (
<Post title={post.title} key={i}/>
);
});
return (
<section>
<SwiperComponent>
{postNodes}
</SwiperComponent>
<button onClick={this.updateData}>update Data</button>
</section>
);
}
});
Fiddle: http://jsbin.com/tocujutaro/1/edit?html,js,output 小提琴: http : //jsbin.com/tocujutaro/1/edit? html, js,输出
The problem is that you're calling swiper synchronously after calling setState
, but the change hasn't been applied to the DOM yet. 问题是您要在调用
setState
之后同步调用swiper,但是更改尚未应用于DOM。 Instead, you should do: 相反,您应该执行以下操作:
this.setState({...}, function () { // Update swiper });
This works because the callback passed to setState
will execute after the state has been applied and the DOM has been updated. 之所以
setState
是因为传递给setState
的回调将在应用状态和更新DOM之后执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.