TypeError:undefined不是对象(正在评估'this.props')-React Nati
TypeError: undefined is not an object (evaluating 'this.props') - React Native
当我在获得json响应后试图导航到下一个视图时,我遇到了这种类型的错误。但如果我的用户不在json响应获取方法的范围内,我的this.props.navigator.push({id:'HomeCaseList')}
会很好地工作。这里有人能帮我吗。
click event
<MKButton
style={{marginLeft:10, marginRight:10,marginTop:10,marginBottom:350,height:40, width:400}}
backgroundColor={'#fff'}
//shadowRadius={2}
//shadowOffset={{width:0, height:2}}
shadowOpacity={.7}
shadowColor="black"
onPress={
// navigator.push();
this.loginToApp.bind(this)
}>
login function
gotoNext() {
fetch('http://url/login/', {
credentials: 'same-origin',
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: 'wasalaa',
password: 'a',
})
}).then(function(response) {
//console.log(response.headers.get('Content-Type'))
//console.log(response.headers.get('Date'))
//console.log(response.status)
//console.log(response.statusText)
//console.log('json',response
//console.log(response.headers.get('JSESSIONID'))
return response.json()
}).then(function(json){
console.log('login response',json)
if(json.message_code === "SUCCESS"){
console.log('user logged')
this.props.navigator.push({
id: 'HomeCaseList',
//sceneConfig: Navigator.SceneConfigs.FloatFromBottom,
});
}
}).catch(function(err){
console.log('error', err)
})
}
在回调中,this
指的是全局范围(在浏览器中,它是node.js中的window
,它是global
,如果您使用strict mode
,它将是undefined
),而不是您的组件,您应该设置this
//....
.then(function(json) {
if (json.message_code === "SUCCESS") {
this.props.navigator.push({
id: 'HomeCaseList',
// sceneConfig: Navigator.SceneConfigs.FloatFromBottom
});
}
}.bind(this))
^^^^^
// ....
相关文章:
- nextProps 总是与 this.props 相同,即使 props 在 componentWillReceiveP
- This.props.dispatch not a function - React-Redux
- TypeError:undefined不是对象(正在评估'this.props')-React Nati
- JavaScript: var {left, ...props} = this.props;
- this.props.history已被弃用(react router)
- ReactJS - {this.props.children} 是未定义的
- 如何从Firebase获取数据并将其放入this.props - React
- ReactJS:this.props.data.map 不是一个函数
- 当 props 扩展时,reactjs 的 this.props 会发生变化
- 反应:如何访问this.props.children中的参考文献
- 为什么在react native中使用this.props.navigator.replace再次渲染同一场景
- 未捕获类型错误:this.props.data.map不是函数(使用超级代理)
- '&&'操作符用{this.props.children &&React.cloneElement (t
- this.props 属性的问题,即使它们存在
- 断言在' this.props.children '中只传递了一个子节点
- React Redux Reducer: 'this.props.tasks.Map不是函数'错误
- React.js对this.props.children的设计实践
- 获取TypeError: this.props在ReactJs上没有定义
- this.props.enableEdit不是执行函数时的函数错误
- 有了reactjs和react-router,我可以显示一个组件或者{this.props.children}吗?