Giter Club home page Giter Club logo

react17-webpack5-starter's Introduction

How to React ^17 with Webpack 5 - Setup Tutorial

Step 1: Following the webpack official website guide

https://webpack.js.org/guides/

Step 2: Following the babel official website

Important

runtime classic | automatic, defaults to classic automatic auto imports the functions that JSX transpiles to. classic does not automatic import anything.

备注:如果runtime不使用automatic, 且组件顶部没有引入import React from 'react';,则会报React不存在错误。

App.js:6 Uncaught ReferenceError: React is not defined
   at App (App.js:6)
   at renderWithHooks (react-dom.development.js:14985)
   at mountIndeterminateComponent (react-dom.development.js:17811)
   at beginWork (react-dom.development.js:19049)
   at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
   at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
   at invokeGuardedCallback (react-dom.development.js:4056)
   at beginWork$1 (react-dom.development.js:23964)
   at performUnitOfWork (react-dom.development.js:22776)
   at workLoopSync (react-dom.development.js:22707)
react-dom.development.js:20085 The above error occurred in the <App> component:

   at App (http://localhost:8080/main.f202e1f6ecde810a7d1a.js:21:3)

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.
logCapturedError @ react-dom.development.js:20085
bootstrap:27 Uncaught ReferenceError: React is not defined
   at App (App.js:6)
   at renderWithHooks (react-dom.development.js:14985)
   at mountIndeterminateComponent (react-dom.development.js:17811)
   at beginWork (react-dom.development.js:19049)
   at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
   at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
   at invokeGuardedCallback (react-dom.development.js:4056)
   at beginWork$1 (react-dom.development.js:23964)
   at performUnitOfWork (react-dom.development.js:22776)
   at workLoopSync (react-dom.development.js:22707)

主要碰到的问题

  1. babel config中"runtime"未设置为"automatic"的问题
  2. ReferenceError: process is not defined,使用如下方式解决
// new webpack.DefinePlugin({
    //   'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
    //   'process.env.WDS_SOCKET_HOST': JSON.stringify(process.env.WDS_SOCKET_HOST),
    //   'process.env.WDS_SOCKET_PORT': JSON.stringify(process.env.WDS_SOCKET_PORT),
    //   'process.env.WDS_SOCKET_PATH': JSON.stringify(process.env.WDS_SOCKET_PATH),
    // })

Reference

[-] https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#manual-babel-setup [-] https://www.robinwieruch.de/minimal-react-webpack-babel-setup

react17-webpack5-starter's People

Contributors

yyvanyang avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.