This is exactly the issue I run into when I try to use react component in a old jquery website.
I tried two ways to make it work:
1. Use webpack to compile the component, my webpack.config.js is something like:
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
main: [
'es6-shim',
'promise-polyfill',
'whatwg-fetch',
'./form_component.js' // Path to your app's entry file
]
},
output: {
path: __dirname,
filename: "bundle.js"
},
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
presets: [
['es2015', {
loose: true,
modules: false
}],
'react'
]
}
}
}
]
},
plugins: [new HtmlWebpackPlugin()]
};
It works but the bundle.js is about 880 KB, which is too large just for a form component.
As the issue is due to babel script is not executed in jquery's globalEval function, I tried to pre-compile it with babel, and use the precompiled js, it also works and the precompiled js is about the same size as the original js file.:
babel form_component.js --presets react,es2015 > precompiled.js