简体   繁体   English

为什么 GitHub 页面不显示我的 React Redux Todo Web 应用程序

[英]Why GitHub Pages does not display my React Redux Todo Web aplication

I wrote a simple React Redux Todo webapp, this is its GitHub repository ;我写了一个简单的 React Redux Todo webapp,这是它的GitHub 存储库 it works fine on my desktop but when I publish it to GitHub pages all I get is a blank page.它在我的桌面上运行良好,但是当我将它发布到GitHub 页面时,我得到的只是一个空白页面。 Interestingly, I added a few console.log statements as page elements are rendered, they show up on the Console, but their page fragments do not.有趣的是,我在渲染页面元素时添加了一些 console.log 语句,它们显示在控制台上,但它们的页面片段没有。

I used the instructions in the Create React App Deployment page, specifically the ones in the GitHub Pages section .我使用了Create React App Deployment页面中的说明,特别是GitHub Pages 部分中的说明。

Not the homepage attribute, predeploy and deploy scritps, and the gh-pages development dependency in the following is my package.json file:不是homepage属性, predeploydeploy脚本,下面的gh-pages开发依赖是我的package.json文件:

{
  "name": "react_todo",
  "author": "Rodrigo Silveira",
  "version": "0.2.0",
  "homepage": "https://rodrigomattososilveira.github.io/react-redux-todo/",
  "private": true,
  "license": "MIT",
  "dependencies": {
    "@material-ui/core": "^4.9.11",
    "@material-ui/icons": "^4.9.1",
    "classnames": "^2.2.6",
    "material-table": "^1.57.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-redux": "^7.2.0",
    "react-scripts": "3.4.1",
    "redux": "^4.0.5",
    "shortid": "^2.2.15",
    "typeface-roboto": "^0.0.75"
  },
  "scripts": {
    "start": "react-scripts start",
    "predeploy": "yarn run build",
    "deploy": "gh-pages -d build",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/jest": "^24.0.0",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "@types/react-redux": "^7.1.7",
    "@types/shortid": "^0.0.29",
    "gh-pages": "^2.2.0",
    "typescript": "~3.7.2"
  }
}

A一个

Also note that I have a React Todo application that I was able to publish to its own GitHub Pages using the exact same recipe I'm using for its [React Redux Todo( https://github.com/RodrigoMattosoSilveira/react-redux-todo ) cousin. Also note that I have a React Todo application that I was able to publish to its own GitHub Pages using the exact same recipe I'm using for its [React Redux Todo( https://github.com/RodrigoMattosoSilveira/react-redux-待办事项)表弟。

How to debug a React application?如何调试 React 应用程序?

In this case, since the error only happened once in production (deployed on Github Pages), it may seem hard to find the source.在这种情况下,由于错误仅在生产中发生一次(部署在 Github 页面上),因此似乎很难找到源头。

First step is to look for error in the console, which there was:第一步是在控制台中查找错误,其中有:

Uncaught Error: Minified React error #152;未捕获的错误:缩小的 React 错误 #152; visit https://reactjs.org/docs/error-decoder.html?invariant=152&args[]=Component for the full message or use the non-minified dev environment for full errors and additional helpful warnings.访问https://reactjs.org/docs/error-decoder.html?invariant=152&args[]=Component获取完整消息,或使用未缩小的开发环境获取完整错误和其他有用的警告。

The full error message from the link is:链接中的完整错误消息是:

Component(...): Nothing was returned from render. Component(...):渲染没有返回任何内容。 This usually means a return statement is missing.这通常意味着缺少 return 语句。 Or, to render nothing, return null.或者,不渲染任何内容,返回 null。

The next step is to figure out how to reproduce the error easily (and hopefully, locally).下一步是弄清楚如何轻松地(并希望在本地)重现错误。

With Create-React-App, you can manually make a production build with yarn build .使用 Create-React-App,您可以使用yarn build手动进行生产构建。 It will compile the final static files into the ./build directory.它将最终的 static 文件编译到./build目录中。 Then, you can start any simple HTTP server on your local machine.然后,您可以在本地计算机上启动任何简单的 HTTP 服务器。 Personally, I use http-server which I have installed globally as a quick debug tool!就个人而言,我使用全局安装的http-server作为快速调试工具!

yarn build
http-server ./build

And you should be able to see the same thing as if it was on Github Pages, but from http://127.0.0.1:8080/ by default.您应该能够看到与 Github 页面上相同的内容,但默认情况下来自http://127.0.0.1:8080/

The last step is the search for the culprit code.最后一步是寻找罪魁祸首代码。 There are possibly better ways to do this, but here's a way that always work.可能有更好的方法可以做到这一点,但这是一种始终有效的方法。 Start with the most minimal code, so comment out all of your own code.从最小的代码开始,所以注释掉你自己的所有代码。

const Root = () => (
  <Provider store={store}>
    {/* <TodoApp /> */}
  </Provider>
);

Then test the build again.然后再次测试构建。 If it works, it means the problem is within <TodoApp /> .如果有效,则表示问题出在<TodoApp />内。 If it doesn't work, it means the problem comes from elsewhere (maybe a configuration, etc).如果它不起作用,则意味着问题来自其他地方(可能是配置等)。

Now in TodoApp , continue this process: comment half of the component, test the build, rince and repeat until you narrow your search to the last place the problem is happening.现在在TodoApp中,继续这个过程:注释一半的组件,测试构建,rince 并重复,直到你将搜索范围缩小到问题发生的最后一个地方。

This is just a simple binary search applied in a debugging context.这只是在调试上下文中应用的简单二进制搜索。


What was wrong in your code?你的代码有什么问题?

That said, I took the time to test the repository and the problem comes from the TodoForm component .也就是说,我花时间测试了存储库,问题来自TodoForm组件

 return ( // <div className="todo-form"> // <input // ref={inputRef} // type="text" // placeholder='Enter new todo' // onChange={event => handleInputOnChange(event)} // onKeyPress={event => handleInputOnKeyPress(event)} // /> // </div> <div className={classes.root} style={{marginTop: '10px', marginBottom: '10px'}}> <form noValidate autoComplete="off"> <div>

The comments at the start of this JSX block are possibly transformed incorrectly in the final build, meaning that the component is missing a valid return value.这个 JSX 块开头的注释可能在最终构建中被错误地转换,这意味着该组件缺少有效的返回值。

The fix is only to remove these comments.修复只是删除这些评论。

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

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