[英]React Rails component: manually triggering a re-render
In Rails, I've got a set of Bootstrap tabs on a page with a React component on each one, ie: 在Rails中,我在页面上有一组Bootstrap选项卡,每个选项卡上都有一个React组件,即:
<div id="tab-1" class="tab-pane">
...
<%= react_component('PeopleWidget', person_id: @person.id) %>
...
</div>
<div id="tab-2" class="tab-pane">
...
<%= react_component('ContentWidget', content_id: @content.id) %>
...
</div>
The components: 组成部分:
Now: 现在:
PeopleWidget
can update a person's permissions in the backend PeopleWidget
可以在后端更新一个人的权限 ContentWidget
to read-only behavior ContentWidget
更改为只读行为 My tactical solution is: 我的战术解决方案是:
tab-2
is shown, I would like to manually trigger ContentWidget
render. tab-2
Bootstrap事件中,我想手动触发ContentWidget
渲染。 ContentWidget
will have proper updated behavior ContentWidget
将具有正确的更新行为 This feels like a workaround but I have a timeline :) 这似乎是一种解决方法,但我有一个时间表:)
I can't figure out how to manually trigger the ContentWidget
to re-render. 我不知道如何手动触发
ContentWidget
重新呈现。 Is this possible? 这可能吗?
You could employ a tactic from Flux: you could set up an event listener in your component, and when the event happens, re-render using forceUpdate
. 您可以采用Flux的策略:您可以在组件中设置事件侦听器,并在事件发生时使用
forceUpdate
重新呈现。
In Flux, components listen to stores. 在Flux中,组件监听商店。 In your case, you could listen to the events triggered by Bootstrap tabs .
在您的情况下,您可以侦听Bootstrap选项卡触发的事件 。
For example, to handle the event when the tab is shown: 例如,在显示选项卡时处理事件:
var ContentWidget = React.createClass({
_handleTabShow: function() {
this.forceUpdate()
// any other reload/re-render code here, too
},
componentWillMount: function() {
// start listening for show event:
$('#tab-2').on('shown.bs.tab', this._handleTabShow)
},
componentWillUnmount: function() {
// clean up the handler when the component is removed:
$('#tab-2').off('shown.bs.tab', this._handleTabShow)
}
// The rest of your component code
// goes here....
})
您可以使用forceUpdate来重新渲染组件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.