即使不使用它也会出现React-router错误

时间:2017-05-12 20:21:18

标签: javascript reactjs react-router react-router-dom

我在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 个答案:

答案 0 :(得分:0)

我建议你:

  1. 删除build * .js文件。
  2. 检查react-router-dom文件中是否package.json
  3. 如果是,那么npm uninstall --save react-router-dom在其他情况下删除node_modules目录,再次安装包,并检查构建。
  4. 您还可以尝试使用browserify构建代码以进行测试:

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