简体   繁体   English

组件未在简单的React.js应用中呈现

[英]Component not rendering in simple React.js app

I'm trying to get a simple "Hello, World"-type React.js app from the book React and React Native running under Node/webpack-dev-server. 我试图从运行在Node / webpack-dev-server下的React和React Native本书中获得一个简单的“ Hello,World”类型的React.js应用程序 I'm not seeing any errors, but the page is blank when I visit it in my browser . 我没有看到任何错误,但是当我在浏览器中访问该页面时该页面为空白

Here's how I tried—and failed—to run the book's very first example : 这是我尝试(但失败)运行本书第一个示例的方法

  1. Install node and npm using these steps (adapted from this gist ): 使用以下步骤安装nodenpm (改编自gist ):

     $ mkdir ~/.local/node-latest-install $ cd ~/.local/node-latest-install $ curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1 $ ./configure --prefix=~/.local $ make -j install $ curl https://www.npmjs.com/install.sh | sh $ node -v; npm -v v7.7.4 4.1.2 
  2. Clone the examples repo from the book: 克隆本书中的示例存储库:

     $ git clone https://github.com/PacktPublishing/React-and-React-Native.git 
  3. Run npm install from the top-level examples folder: 从顶级示例文件夹运行npm install

     $ cd React-and-React-Native $ npm install 
  4. Attempt to compile/run hello-jsx example: 尝试编译/运行hello-jsx示例:

     $ cd Chapter02/hello-jsx $ node ../../node_modules/webpack-dev-server/bin/webpack-dev-server.js ... ERROR in ./main.js Module build failed: Error: React Hot Loader: The Webpack loader is now exported separately. If you use Babel, we recommend that you remove "react-hot-loader" from the "loaders" section of your Webpack configuration altogether, and instead add "react-hot-loader/babel" to the "plugins" section of your .babelrc file. If you prefer not to use Babel, replace "react-hot-loader" or "react-hot" with "react-hot-loader/webpack" in the "loaders" section of your Webpack configuration. at Object.warnAboutIncorrectUsage (/home/evadeflow/.local/React-and-React-Native/node_modules/react-hot-loader/lib/index.js:7:11) @ multi main webpack: Failed to compile. 
  5. Edit hello-jsx/webpack.config.js to work around the above error: 编辑hello-jsx/webpack.config.js以解决以上错误:

     diff --git a/Chapter02/hello-jsx/webpack.config.js b/Chapter02/hello-jsx/webpack.config.js index 312f152..592e767 100644 --- a/Chapter02/hello-jsx/webpack.config.js +++ b/Chapter02/hello-jsx/webpack.config.js @@ -13,7 +13,7 @@ module.exports = { { test: /\\.jsx?$/, exclude: /node_modules/, - loaders: ['react-hot', 'babel?presets[]=es2015'], + loaders: ['react-hot-loader/webpack', 'babel?presets[]=es2015'], }, ], }, 
  6. Recompile/re-run: 重新编译/重新运行:

     $ node ../../node_modules/webpack-dev-server/bin/webpack-dev-server.js http://localhost:8080/webpack-dev-server/ webpack result is served from / content is served from /home/evadeflow/.local/React-and-React-Native/Chapter02/hello-jsx Hash: 590282c761dccbb5710a Version: webpack 1.14.0 Time: 1260ms Asset Size Chunks Chunk Names main-bundle.js 978 kB 0 [emitted] main chunk {0} main-bundle.js (main) 923 kB [rendered] [0] multi main 52 bytes {0} [built] [1] (webpack)-dev-server/client?http://0.0.0.0:8080 3.97 kB {0} [built] [2] /home/evadeflow/.local/React-and-React-Native/~/url/url.js 23.3 kB {0} [built] [3] /home/evadeflow/.local/React-and-React-Native/~/url/~/punycode/punycode.js 14.6 kB {0} [built] <... lots of similar output omitted ...> [251] /home/evadeflow/.local/React-and-React-Native/~/react-dom/lib/ReactDOMUnknownPropertyHook.js 4.32 kB {0} [built] [252] /home/evadeflow/.local/React-and-React-Native/~/react-dom/lib/ReactDOMNullInputValuePropHook.js 1.37 kB {0} [built] [253] /home/evadeflow/.local/React-and-React-Native/~/react-dom/lib/ReactDOMInvalidARIAHook.js 3.14 kB {0} [built] webpack: Compiled successfully. 

After the last step, node is running. 最后一步之后, node正在运行。 However, I see a blank page when I hit localhost:8080 in my browser: 但是,当我在浏览器中点击localhost:8080时,我看到一个空白页:

在此处输入图片说明

Am I doing something wrong? 难道我做错了什么?

Try the following: 请尝试以下操作:

1) Install webpack both globally and locally, ie at the base directory of the code do the following: 1)在全局和本地安装webpack,即在代码的基本目录中执行以下操作:

npm install webpack --global
npm install webpack --save-dev

2) Install the webpack dev server, also both globally and locally: 2)在全局和本地安装webpack开发服务器:

npm install webpack-dev-server --global
npm install webpack-dev-server --save-dev

3) Update the webpack.config.js files, in particular the "loader" line, as follows: 3)更新webpack.config.js文件,尤其是“ loader”行,如下所示:

(A) Replace 'react-hot' with 'react-hot-loader/webpack' (A)将'react-hot'替换为'react-hot-loader / webpack'

(B) Replace 'babel' with 'babel-loader' (B)将“ babel”替换为“ babel-loader”

So for example, the "hello-jsx" file here is the line in the OLD, ORIGINAL webpack.config.js file: 因此,例如,此处的“ hello-jsx”文件是原始的原始webpack.config.js文件中的行:

loaders: ['react-hot', 'babel?presets[]=es2015'],

And here is the line in the NEW, UPDATE webpack.config.js file: 这是NEW,UPDATE webpack.config.js文件中的行:

loaders: ['react-hot-loader/webpack', 'babel-loader?presets[]=es2015'],

4) Run the webpack dev server (from the subdirectory) with the hot and inline options, eg 4)使用hot和inline选项运行webpack dev服务器(从子目录),例如

$ pwd
[whatever_base_directory]/Chapter02/hello-jsx
$ webpack-dev-server --hot --inline

I've resolved a lot of these issues. 我已经解决了很多这些问题。 Try pulling the latest . 尝试拉最新的

The two major things I've fixed are: 我已修复的两个主要问题是:

  1. Using Webpack 2 as a dependency and adding instructions for installing webpack/webpack-dev-server globally as part of the initial setup. 使用Webpack 2作为依赖项,并在初始设置中添加有关全局安装webpack / webpack-dev-server的说明。
  2. Simplified webpack configuration for each example. 每个示例的简化的webpack配置。 We only need babel-loader and one entry point. 我们只需要babel-loader和一个入口点。

Give it a try, let me know if it's any easier to get going. 试试看,让我知道它是否更容易上手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM