最近开始使用学习 React Native,再次记录所遇到的错误
- Cannot add a child that doesn't have a YogaNode to a parent without a measure function! (Trying to add a 'ReactRawTextShadowNode' to a 'LayoutShadowNode')
代码如下
App.js
export default class App extends Component<{}> {
render() {
if (Platform.OS === 'android') {
return <ViewTest style={{flex:1}}/>
}
}
}
// ViewTest.js
export default class ViewTest extends Component{
render(){
return (
<View style={[styles.container,styles.flex]}>
<View style={styles.item}>
// 第1列
<View style={[styles.flex,styles.center]}>
<Text style={styles.textStyle}>酒店</Text>
</View>
</View>
</View>
)
};
}
问题原因及解决方案: // 这不是 ReactNative 在 JSX 中的注释方式,注释方式为 {/**/},更改注释后就好了。
- Navigator 使用错误。
这两天在使用
Navigator时一直报错。 错误一:
Invariant Violation: Navigator is deprecated and has been removed from this package.
It can now be installed and imported from `react-native-deprecated-custom-components` instead of `react-native`.
Learn about alternative navigation solutions at http://facebook.github.io/react-native/docs/navigation.html

错误原因及解决方案:
错误原因是因为 React Native 在 0.44 以后已经把 Navigator 废弃掉了,不能使用 import {Navigator} from 'react-native'; 进行导入了。根据提示需要导入 react-native-deprecated-custom-components
解决步骤:
1.进入到项目的根目录下。
2.使用命令行 :npm install react-native-deprecated-custom-components --save
3.在使用Navigator的地方引用。引用方式:import {Navigator} from "react-native-deprecated-custom-components"
注意:在引用时 一定在导入 Navigator的时候加上 {} 要不然会报错。
安装如果出错的解决:
方案一:使用命令:npm i react-native-deprecated-custom-components --save
如果此方法还不能解决。
方案二:使用yarn命令(前提是自己有yarn的配置环境)yarn add react-native-deprecated-custom-components --save 。
Navigator 错误二:
我在上述引用 react-native-deprecated-custom-components 后,使用 Navigator 还是会报错。
错误提示: Cannot read property 'shape' of undefined

错误还是因为 React Native 在 0.44 以后已经把 Navigator 废弃掉了。
现在要改为用 React Navigation。详情请参考github react-native, React Native 使用文档,新手理解Navigator的教程
PS : 这个错误卡了我两天啊。。。
3.PropTypes 错误 Cannot read property 'string' of undefined

PropTypes 时导入出错。import React, {Component,PropTypes} from 'react';
原因是 : React v15.5 起就被移除了,需要改用 prop-types 库代替。
解决方案:
- 进入项目根目录,安装 prop-types 库:
npm install --save prop-types - 引入:
import PropTypes from 'prop-types';