可能报错: Element type is invalidjavascript
ES5的导出导入语法:css
module.exports = Page2;
var NaviBar = require('./NaviBar');
复制代码
ES6的导入导出语法:html
export default class Page2 extends Component import NaviBar from './NaviBar';
复制代码
Demo中使用了错误的导入方法java
import { AppRegistry } from 'react-native';
import { FirstView } from './Demo/ViewDemo.js';
复制代码
正确的是node
import { AppRegistry } from 'react-native';
import FirstView from './Demo/ViewDemo.js';
复制代码
导出带有default,export default
,引用时能够不加大括号。导出没有带default,引用时要加大括号react
属性前面的三个点(...props),是延展操做符。 在React中,延展操做符通常用于属性的批量赋值。好比es6
var props = {};
props.foo = x;
props.bar = y;
var component = <Component {...props} />; 复制代码
等同于chrome
var props = {};
props.foo = x;
props.bar = y;
var component = <Component foo={x} bar={y} />; 复制代码
使用延展操做符的好处是,当你的属性有所增长的时候,组件中的使用不须要去增长对应的属性。npm
state
getInitialState(){
return {
currentPage: 0,
}
},
复制代码
constructor(props) {
super(props);
this.state = {
selectedTab: tabBarItems[0].title,
};
}
复制代码
props
getDefaultProps(){
return {
duration: 3000,
}
},
复制代码
static defaultProps = {
visible: false,
}
复制代码
<TouchableOpacity
//按下时透明度变化
activeOpacity={0.5}
onPress={() => this.activeEvent('点击')}
>
<Text style={{
color: 'white',
fontSize: 20,
}}>登陆</Text>
</TouchableOpacity>
activeEvent(event) {
Alert.alert(event)
}
复制代码
<View style={styles.contanier} >
<Text onPress={this.timePlus.bind(this)}>点击 </Text>
</View>
timePlus() {
......
}
复制代码
arr = [2,4,5,7,8]
arr.forEach((item,index,array)=>{
})
复制代码
var res = arr.map((item,index,arr )=>{
return item*10;
})
console.log(res);//-->[20, 40, 50, 70, 80]; 原数组拷贝了一份,并进行了修改
console.log(arr);//-->[2, 4, 5, 7, 8]; 原数组并未发生变化
复制代码
delete arr[1]
console.log(arr);//[2, undefined, 5, 7, 8]
复制代码
arr.splice(1,1)
console.log(arr)//[2, 5, 7, 8]
复制代码
arr.splice(1,2)
console.log(arr)//[2, 7, 8]
复制代码
1.使用回调函数
setTimeout(function() {
console.log('Hello'); // 1秒后输出"Hello"
setTimeout(function() {
console.log('Hi'); // 2秒后输出"Hi"
}, 1000);
}, 1000);
复制代码
2.Promise对象
var waitSecond = new Promise(function(resolve, reject) {
setTimeout(resolve, 1000);
});
waitSecond
.then(function() {
console.log("Hello"); // 1秒后输出"Hello"
return waitSecond;
})
.then(function() {
console.log("Hi"); // 2秒后输出"Hi"
});
复制代码
3.async/await
waitSecond() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('sucess');
}, 600);
});
}
复制代码
async test() {
const result = await this.waitSecond();
console.warn(result);
}
this.test();
复制代码
shouldComponentUpdate
方法中setState
新生命周期
static getDerivedStateFromProps(nextProps, prevState) {
const {type} = nextProps;
// 当传入的type发生变化的时候,更新state
if (type !== prevState.type) {
return {
type,
};
}
// 不然,对于state不进行任何操做
return null;
}
复制代码
parent: {
backgroundColor:'blue',
marginTop: 20,
paddingTop: 50,
paddingBottom: 50,
},
text: {
fontSize: 20,
}
复制代码
父组件默认竖直显示,此时Text
组件默认横向全屏
item: {
backgroundColor:'blue',
marginTop: 20,
paddingTop: 50,
paddingBottom: 50,
justifyContent: 'center',
flexDirection: 'row'
},
text: {
fontSize: 20,
backgroundColor: 'red',
}
复制代码
添加属性justifyContent: 'center', flexDirection: 'row'
,便可让Text自适应大小,而且水平居中
只要不在render函数的返回组件中使用this.props或者this.setState,那么this就做用于当前操做对象
如何在render函数的return中使用this.props或者this.setState呢?
this.函数名 = this.函数名.bind(this)
{this.函数名.bind(this)}
由于在React 15.5.0 以后的版本中,将PropTypes从React库中废除掉了,所以在React 15.5.0 以后的版本,咱们就不要再已这种方式导出PropTypes了
import React, { Component, PropTypes} from 'react'
复制代码
更换为:
import { ViewPropTypes } from 'react-native'
import PropTypes from 'prop-types'
复制代码
安装prop-types库
npm install prop-types --save
复制代码
具体使用
import React, {Component} from 'react';
import {
Text,
View
} from 'react-native';
import PropTypes from 'prop-types'
export default class PropsTest extends Component{
//设置默认属性方法一
static defaultProps={
name:"小红",
age:20,
sex:"男"
}
//类型检测方法一
/*static propTypes={ name:PropTypes.string, age:PropTypes.number, sex:PropTypes.string.isRequired, }*/
render(){
return (
<View> <Text style={{fontSize:20,backgroundColor:'red'}}>你好: {this.props.name}</Text> <Text style={{fontSize:20,backgroundColor:'red'}}>年龄: {this.props.age}</Text> <Text style={{fontSize:20,backgroundColor:'red'}}>性别: {this.props.sex}</Text> </View>
);
}
}
//设置默认属性方法二
/*PropsTest.defaultProps={ name:"小红", age:20, sex:"男" }*/
//类型检测方法二
PropsTest.propTypes={
name:PropTypes.string,
age:PropTypes.number,
sex:PropTypes.string.isRequired,
}
复制代码
# 数组类型
PropTypes.array
# 布尔类型
PropTypes.bool
# 函数类型
PropTypes.func
# 数值类型
PropTypes.number
# 对象类型
PropTypes.object
# 字符串类型
PropTypes.string
# 规定prop为必传字段
PropTypes.func.isRequired
# prop可为任意类型, 任意不为空对象
PropTypes.any
#数组中元素的类型
PropTypes.any. arrayOf()
#React 元素
PropTypes.element
#类实例
PropTypes.instanceOf()
#每一个值的类型都是基本类型
PropTypes.node
#参数是数组, 指定传的数据为数组中的值,能够当作枚举类型使用
PropTypes.oneOf()
e.g-好比
color: PropTypes.oneOf(['red', 'blue', 'black'])
# 参数是数组, 指定传的数据为数组中的类型
PropTypes.oneOfType()
# 指定对象类型内部的结构定义
PropTypes.shape()
e.g-好比
model:PropTypes.shape(
{
id: PropTypes.string,
name: PropTypes.string
}
)
复制代码
经过组件类的 defaultProps 属性为 props 设置默认值(static里面的赋值操做会在app一运行 就会调用),实例以下:
export default class PropComponent extends Component {
static defaultProps={
name: '张三',
sex:'man',
tel:'13866666666'
}
}
复制代码
声明defaultProps 属性后,在遇到没有被赋值的属性时,就会读取默认属性值。
2.0之前版本:
复制代码
新版本:
复制代码
StackNavigator --> createStackNavigator
TabNavigator --> createBottomTabNavigator 也可使用
createStackNavigator 返回的结果须要再用createAppContainer包裹 const MainNavigator = createAppContainer(stackNavigator );\
3.x版本必须安装react-native-gesture-handler
使用方法参考:https://reactnavigation.org/docs/en/3.x/getting-started.html
和https://zhuanlan.zhihu.com/p/67967551
注意: 多个navigation嵌套
假设第一个navigation配置了A和B,A里面配置了第二个navigation,C和D。在C想要跳转到A时,应该使用第一个navigation的navigate方法
在用chrome调试页面时,一刷新页面总会自动打断点。全部的breakpoints都已经取消。把调试面板上的第5个按钮从Deactivate breakpoints 改为Activate breakpoints.解决问题