React-Native之undefined is not an object
来源:互联网 发布:cf湖北一区阿玛尼网络 编辑:程序博客网 时间:2024/06/03 20:17
刚学习rn,有很多不理解的地方,常常会报undefined is not an object这个错,然后在不断的修改和试错的情况下
,发现只要发生这个情况就有this存在,先贴出一段错误的代码
export default class Test extends Component { constructor(props) { super(props) this.state = { header: "我是header", _data: [ {key: 'a'}, {key: 'b'}, {key: 'c'}, {key: 'd'}, {key: 'd'}, ] } } _header() { return ( <Text style={{backgroundColor: "red", height: 100,}}> {this.state.header} </Text> ); } render() { return ( <View> <FlatList ListHeaderComponent={this._header}//header头部组件 data={this.state._data} renderItem={({item}) => <Text style={{alignSelf: "center", flex: 1, height: 30}}>{item.key}</Text>} /> </View> ); }}
报错如图
说是this.state.header这地方错误,刚接触这些很郁闷,引用state里面的值不都是这么引入的吗,
我试着写了个简单的demo
export default class Test extends Component { constructor(props) { super(props) this.state = { header: "我是header", } } render() { return ( <View> <Text>{this.state.header}</Text> </View> ); }}
完全是可以的,这让我这个初入rn的初学者头疼了,后来我想,会不会和这个this有关呢,
我试着删除{this.state.header},随便加点固定值,是完全可以的,后来我想,这个_header函数是FlatList组件的
一个子组件,会不会是_header()函数的this指向的是FlatList组件呢?我要如何拿到全局的this呢?
后来看了下资料,找到了解决方法,就是给_header()函数绑定全局的this,默认的this是指向是父组件的
export default class Test extends Component { constructor(props) { super(props) this.state = { header: "我是header", _data: [ {key: 'a'}, {key: 'b'}, {key: 'c'}, {key: 'd'}, {key: 'd'}, ] } //将全局的this绑定到_header函数 this._header=this._header.bind(this) } _header() { return ( <Text style={{backgroundColor: "red", height: 100,}}> {this.state.header} </Text> ); } render() { return ( <View> <FlatList ListHeaderComponent={this._header}//header头部组件 data={this.state._data} renderItem={({item}) => <Text style={{alignSelf: "center", flex: 1, height: 30}}>{item.key}</Text>} /> </View> ); }}
总结
1、子组件指向的this属于子组件,与父组件无关
2、子组件不能去父组件更新状态
3、如果要在子组件更新父组件的状态,给子组件绑定父组件的this
阅读全文
0 0
- React-native:Undefined is not an object
- React-Native之undefined is not an object
- 《React-Native系列》17、 RN中this所引起的undefined is not an object错误
- 《React-Native系列》17、 RN中this所引起的undefined is not an object错误
- React Native 出现undefined is not an object (evaluating 'this.props.navigator')
- React-Native 0.43.4 undefined is not an object (evaluating '_props[registrationName]') 正确的解决姿势
- react-native-baidu-map undefined is not an object(evaluting 'a.PropTypes.bool')
- React Native开发坑之null is not an object (evaluating 'inst.componentWillReceiveProps')
- React Native ListView的Item设置点击事件时null is not an object
- 运行React Native示例出现null is not an object (evaluating 'this.state.text')
- React Native报错unddefined is not an object(evaluating ‘_reactnative.propTypes’)解决办法
- 【ReactNative】undefined is not an object(evaluating 'this.props.navigator')
- undefined is not an object(evaluating 'this._component.getScrollableNode')
- undefined is not an object (evaluating 'this.props.navigator.push')
- undefined is not an object (evaluating 'dataSource.rowIdentities')
- [ReactNative]undefined is not an object (evaluating 'ImagePickerManager.showImagePicker')
- ReactNative报错:undefined is not an object(evaluating 'RCTCameraRollManager.getPhotos')
- 在TabNavigator中使用Navigation报错undefined is not an object (this.props.navigation.navigate)
- 智能小车38:晶振原理与串口的使用
- java String 的equals方法
- 《流畅的Python》
- 应用开发者必须了解的Kubernetes网络二三事
- Python中os与sys两模块的区别
- React-Native之undefined is not an object
- MongoDB 聚合管道(Aggregation Pipeline)
- BMC远程安装系统
- Effective Java中文第四章第15节(个人渣翻)
- UITableViewCell
- 读取文件大小的length()方法,available()方法,getChannel().size()的区别
- 支持向量机学习笔记(1)
- jni教程与技术手册
- 10.3视频帧处理