0

我对 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 编译主要的 react 文件,我将其修改为尽可能简单,只是为了找出恼人的错误来自哪里:

应用程序.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'
            }
        ]
    }
};
4

1 回答 1

0

我建议你:

  1. 删除构建 *.js 文件。
  2. 检查不在文件中react-router-dompackage.json
  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
于 2017-05-12T20:36:30.423 回答