导出您的js代码,然后将其导入您的react组件中。
export function toggleIcon() {
$('.toggle-icon').on('click', function() {
if ($(this).hasClass("active")) {
$(this).removeClass('active');
$(this).next().slideUp();
} else {
$(this).find('.toggle-icon').removeClass('active');
$(this).find('ul').slideUp();
$(this).addClass('active').next().slideDown();
}
});
}
然后,您可以将其导入您的react组件中。
// custom is the path to the file that holds your js code
import { toggleIcon } from './custom';
然后在您的react组件中调用它,例如,在诸如之类的react生命周期方法中componentDidMount
。
componentDidMount() {
toggleIcon();
}
另一种(更快)的方法是require
在您的react组件中使用来加载js代码。
require('./custom');