react-router 2.5 context undefiend replaceState deprecated

来源:互联网 发布:剑网三咩太捏脸数据 编辑:程序博客网 时间:2024/06/11 02:48

今天开始测试react-router 2.5 ,按网上的一般教程,一些用法都提示不建议使用了,还有context在子组件身上拿不到,一直提示undefined
例如:MyComp.contextTypes = {
router: React.PropTypes.func.isRequired
}

但是这样设置后会报错,也是醉了,
最后直接在最外层组件(APP)中赋值全局变量,window.gRouter=this.props.router,后面直接在子组件里直接操作这个gRouter就可以拿到router了,简单直接。

在react-router 2.5中不建议使用pushState/replaceState改成push/replace 参数也有变化
pushState(state, path, query)==》 push({path, query, state});
replaceState(state, path, query)==》replace({path, query, state});

var checkLogin=function(nextState, replace){
if(localStorage.login==undefined||localStorage.login ==’false’){ //模拟登陆
replace({path:’/’,query:{},state:{nextPathname:nextState.location.pathname}});//新的写法
//replaceState({nextPathname:nextState.location.pathname},’/’);//旧的写法
}
}

0 0
原创粉丝点击