react-router:警告:上下文类型失败:未指定必需的上下文`router`

时间:2015-12-27 08:06:17

标签: reactjs react-router

我克隆了最新的react-router回购:

一切正常,

我将auth-flow示例复制到我自己的文件夹中以运行测试演示

但它收到了这个错误:

enter image description here

webpack.config.js

module.exports = {
entry: './app.js',
output: {
    path: __dirname + '/build/',
    filename: 'bundle.js'
},
module: {
    loaders: [
        {
            test: /\.jsx?$/,
            exclude: /(node_modules|bower_components)/,
            loader: 'babel',
            query: {
                presets: ['react', 'es2015']
            }
        }
    ]
}
};

的package.json

{
    "name": "reacttest",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "babel-core": "^6.3.26",
        "babel-loader": "^6.2.0",
        "babel-preset-es2015": "^6.3.13",
        "babel-preset-react": "^6.3.13",
        "history": "^1.17.0",
        "react": "^0.14.3",
        "react-dom": "^0.14.3",
        "react-router": "^1.0.3",
        "webpack": "^1.12.9"
    }
}

我错过了什么 如何解决这个错误?

2 个答案:

答案 0 :(得分:0)

在您Login组件中,您尝试访问this.context.router,但未指明Login应该有权访问它。

class Login extends React.Component {
   static contextTypes = {
       router: React.PropTypes.func.isRequired
   }

   render() {}
}

答案 1 :(得分:0)

应改为使用History mixin。

结帐到最新分支以查看正确的示例代码