![](/img/trans.png)
[英]How To Call A JavaScript Function Returned From NodeJS in ReactJS JSX
[英]How do you call a jQuery function on a ReactJS element from .jsx script?
我剛剛開始學習ReactJS,這件事發生在我身上。
例如:
我想對reactjs元素執行的函數:
function initializeInput(selector, color) {
// just an example function
$(selector).css("font-size", "21pt");
}
和我的.jsx
文件的一部分:
var myInput = React.createClass({
componentDidMount: function () {
initializeInput("#" + this.props.inputId);
},
render: function() {
return (
<input type="text" value="text goes here"
name={this.props.inputName}
id={this.props.inputId}/>
);
}
});
這個函數被成功調用,但沒有任何反應,似乎事情不僅僅是用jQuery和React這樣做。 將它們混合起來甚至是好的嗎? 謝謝。
Jquery與Reactjs非常合作。 您可以在反應渲染之后調用jquery函數,即在componentDidMount和中
你可以為此做出反應。 讓我們說你想要一個工具提示
var myInput = React.createClass({
componentDidMount: function () {
$(this.refs.tooltip).tooltip();
},
render: function() {
return (
<input ref="tooltip" type="text" value="text goes here"
name={this.props.inputName}
id={this.props.inputId}/>
);
}
});
首先,您需要渲染組件元素( render
函數),然后在componentDidMount
處理程序中調用您的jquery代碼
我相信使用this.refs
就是這樣。 我認為你不應該需要React.findDomNode
。 更多關於React文檔的引用。
var myInput = React.createClass({
componentDidMount: function () {
initializeInput(this.refs.inputId);
},
render: function() {
return (
<input
ref="myInput"
type="text"
value="text goes here"
name={this.props.inputName}
id={this.props.inputId} />
);
}
});
這對於第三方庫(如jQuery,d3,做畫布等)非常有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.