Javascript reactjs渲染方法完成后执行javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29166827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute javascript after reactjs render method has completed
提问by p_mcp
I have a reactjs component:
我有一个 reactjs 组件:
var com1 = React.createClass({
render: function() {
return (
<a href='#'>This is a text</a>
);
}
});
I want to execute Javascript/Jquery once rendering of this componenet has completed. Simply adding Javascript to the render method doesn't seem to work.
一旦这个组件的渲染完成,我想执行 Javascript/Jquery。简单地将 Javascript 添加到 render 方法似乎不起作用。
How would I achieve this?
我将如何实现这一目标?
回答by Roman Liutikov
Use componentDidMountmethod to run code after initial render and componentDidUpdateto run code after each update of component's state.
使用componentDidMount方法来运行代码后,初始渲染和componentDidUpdate到组件的状态的每次更新后运行代码。
回答by kakigoori
See the documentation here: https://facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount
请参阅此处的文档:https: //facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount
In the component lifecycle, you can define a callback for componentDidMount -- i.e., the render function has completed and the resulting elements have been inserted into the DOM.
在组件生命周期中,您可以为 componentDidMount 定义一个回调——即渲染功能已完成并且生成的元素已插入到 DOM 中。

