ReactJS按钮或webpack创建的提交函数无法工作

798qvoo8  于 2023-10-19  发布在  Webpack
关注(0)|答案(3)|浏览(188)

所以我正在创建一个ReactJS应用程序并为其配置webpack。下面是我的webpack配置:
webpack.config.js

  1. const webpack = require('webpack');
  2. const HtmlWebPackPlugin = require( 'html-webpack-plugin' );
  3. const path = require( 'path' );
  4. const dotenv = require('dotenv');
  5. const CopyWebpackPlugin = require('copy-webpack-plugin');
  6. const env = dotenv.config().parsed;
  7. const envKeys = Object.keys(env).reduce((prev, next) => {
  8. prev[`process.env.${next}`] = JSON.stringify(env[next]);
  9. return prev;
  10. }, {});
  11. module.exports = {
  12. context: __dirname,
  13. entry: ['./src/js/index.js','./src/sass/index.scss'],
  14. output: {
  15. path: path.resolve( __dirname, 'dist' ),
  16. filename: 'main.js',
  17. publicPath: '/',
  18. },
  19. devtool: 'eval-source-map',
  20. devServer: {
  21. historyApiFallback: true,
  22. contentBase: path.join(__dirname, 'dist'),
  23. },
  24. module: {
  25. rules: [
  26. {
  27. test: /\.js$/,
  28. loader: 'babel-loader',
  29. exclude: [/node_modules/],
  30. query: {
  31. presets: ['@babel/react', "@babel/preset-env"]
  32. }
  33. },
  34. {
  35. test: /\.jsx?$/,
  36. loader: 'babel-loader',
  37. exclude: [/node_modules/],
  38. query: {
  39. presets: ['@babel/react', "@babel/preset-env"]
  40. }
  41. },
  42. {
  43. test: /\.scss$/,
  44. use: ["style-loader", "css-loader", "sass-loader"]
  45. },
  46. {
  47. test: /\.css$/,
  48. use: ['style-loader', 'css-loader'],
  49. },
  50. {
  51. test: /\.(gif|png|jpe?g|svg|webp)$/i,
  52. use: [
  53. 'file-loader',
  54. {
  55. loader: 'image-webpack-loader',
  56. options: {
  57. bypassOnDebug: true, // [email protected]
  58. disable: true, // [email protected] and newer
  59. },
  60. },
  61. ],
  62. }
  63. ]
  64. },
  65. plugins: [
  66. new HtmlWebPackPlugin({
  67. template: path.resolve( __dirname, 'public/index.html' ),
  68. filename: 'index.html',
  69. }),
  70. new webpack.ProvidePlugin({
  71. "React": "react",
  72. }),
  73. new CopyWebpackPlugin({
  74. patterns: [
  75. { from: 'public/assets' }
  76. ]
  77. }),
  78. new webpack.DefinePlugin(envKeys),
  79. ]
  80. };

一切都很顺利,直到我意识到我所有的按钮都不起作用,我检查了所有的函数和语法,以确保如果我得到它的错误,但事实证明我没有犯错误
这是我的按钮的功能:

  1. const submitMail = e => {
  2. console.log('here')
  3. alert('here')
  4. e.preventDefault()
  5. e.stopPropagation()
  6. e.nativeEvent.stopImmediatePropagation();
  7. console.log('test')
  8. }

我试着在我的测试按钮调用它,但它不会工作,它没有发送警报,它甚至不会记录我所有的控制台。日志

  1. <button onClick={e => submitMail(e)}>test</button>

我还试图把它们放在我的表单中,在onSubmit沿着另一个按钮来触发它,看看它是否会触发“console.log”,这也不起作用:

  1. <form onSubmit={e => submitMail(e)}>
  2. <input id="banner-text-input-email-input-wrapper-email-input" placeholder="Enter your email here"/>
  3. <input id="banner-text-input-email-input-wrapper-submit-input" onClick={()=>console.log('hi')} type="button" value="Button Value"/>
  4. </form>

注意:我已经尝试运行正常的react应用程序:
react-script启动
所有的按钮都能用!在这一点上,我只能认为它不工作的原因是因为我的webpack配置,这里也是我的webpack命令以防万一:
webpack serve --open --mode=development --config= webpack.js.js--hot

q35jwt9p

q35jwt9p1#

好吧,事实证明,我在这件事上非常愚蠢,我对webpack非常陌生,所以我想我需要把

  1. <script type="text/javascript" src="main.js"></script>

在我的public/index.html文件中
我删掉那部分后一切都很顺利

b1payxdu

b1payxdu2#

而不是

  1. <button onClick={e => submitMail(e)}>test</button>

你有没有试着

  1. <button onClick={submitMail}>test</button>

如果我没弄错的话,它会隐式地发送事件...

5fjcxozz

5fjcxozz3#

如果我们升级Webpack V5,任何点击事件都不起作用。因此,如果您使用,我们需要删除public/index.html中的标记。应该能用

相关问题