繁体   English   中英

即使没有使用React-router错误

[英]React-router error even without using it

我在react-router-dom上遇到了一些问题,特别是:

warning.js?8a56:36 Warning: Failed prop type: The prop `history` is marked as required in `Router`, but its value is `undefined`.
    in Router
printWarning @ warning.js?8a56:36
warning @ warning.js?8a56:60
checkReactTypeSpec @ checkReactTypeSpec.js?d08c:80
validatePropTypes @ ReactElementValidator.js?a599:161
createElement @ ReactElementValidator.js?a599:213
(anonymous) @ app.jsx?0ec4:14
(anonymous) @ bundle.js:1289
__webpack_require__ @ bundle.js:20
(anonymous) @ bundle.js:66
(anonymous) @ bundle.js:69
Router.js?a4aa:31 Uncaught TypeError: Cannot read property 'location' of undefined
    at new Router (eval at <anonymous> (bundle.js:794), <anonymous>:39:52)
    at eval (eval at <anonymous> (bundle.js:2335), <anonymous>:295:18)
    at measureLifeCyclePerf (eval at <anonymous> (bundle.js:2335), <anonymous>:75:12)
    at ReactCompositeComponentWrapper._constructComponentWithoutOwner (eval at <anonymous> (bundle.js:2335), <anonymous>:294:16)
    at ReactCompositeComponentWrapper._constructComponent (eval at <anonymous> (bundle.js:2335), <anonymous>:280:21)
    at ReactCompositeComponentWrapper.mountComponent (eval at <anonymous> (bundle.js:2335), <anonymous>:188:21)
    at Object.mountComponent (eval at <anonymous> (bundle.js:387), <anonymous>:46:35)
    at ReactCompositeComponentWrapper.performInitialMount (eval at <anonymous> (bundle.js:2335), <anonymous>:371:34)
    at ReactCompositeComponentWrapper.mountComponent (eval at <anonymous> (bundle.js:2335), <anonymous>:258:21)
    at Object.mountComponent (eval at <anonymous> (bundle.js:387), <anonymous>:46:35)

我想知道那可能来自哪里。 我从编译文件中完全删除了它。

我使用webpack来编译主要的响应文件,我将其修改为尽可能简单,以便找出令人讨厌的错误来自何处:

app.js

import ReactDom from 'react-dom';
const Error = () => <h1>Error</h1>;


ReactDom.render(
    <Error/>
    ,
    document.getElementById('react-app'));

所以我基本上不再在任何地方使用react-router-dom了,但是由于某种原因,错误仍然存​​在。

Webpack配置:

module.exports = {
    entry: './client/src/app.js',
    devtool: '#eval-source-map',
    output: {
        path: __dirname + '/client/dist/',
        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /(node_modules|app-server.js)/,
                loader: 'babel-loader'
            }
        ]
    }
};

我建议你:

  1. 删除构建* .js文件。
  2. package.json文件中选中Is not react-router-dom
  3. 如果是,则使用npm uninstall --save react-router-dom在其他情况下)删除node_modules目录,再次安装软件包,然后检查build。

您也可以尝试使用Browserify构建代码进行测试:

npm install -g browserify
browserify ./client/src/app.js -o ./client/dist/bundle.js -t [ babelify --presets [ es2015 react ] ] -v

暂无
暂无

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

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