gpt4 book ai didi

javascript - 如何从 .jsx 脚本中调用 ReactJS 元素上的 jQuery 函数?

转载 作者:搜寻专家 更新时间:2023-11-01 05:04:04 25 4
gpt4 key购买 nike

我刚刚开始学习 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 配合得很好。您可以在 componentDidMount 和

react 渲染后调用 jquery 函数

您可以对此使用react。比方说你想要一个工具提示

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}/>
);
}
});

关于javascript - 如何从 .jsx 脚本中调用 ReactJS 元素上的 jQuery 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38025880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com