Skip to content

Commit 44d8068

Browse files
committed
first commit
1 parent 887db1c commit 44d8068

File tree

5 files changed

+319
-1
lines changed

5 files changed

+319
-1
lines changed

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,41 @@
11
# ReactNativeNote
2-
记录一些在学习ReactNative过程中遇到的问题和它们的解决方案
2+
3+
[![Travis](https://img.shields.io/badge/build-passing-brightgreen.svg)](https://github.com/linsir6) [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) [![GitHub (pre-)release](https://img.shields.io/badge/release-v1.0.0-ff69b4.svg)](https://github.com/linsir6/Android-Notes/releases) [![User](https://img.shields.io/badge/user-linsir-yellow.svg)](https://github.com/linsir6)
4+
5+
6+
## ReactNative
7+
8+
- [ReactNative入门](/ReactNative相关/ReactNative入门.md)
9+
- [短信验证码倒计时控件](/ReactNative相关/短信验证码倒计时控件.md)
10+
- [ReactNative报错记录](/ReactNative相关/ReactNative报错记录.md)
11+
12+
----
13+
14+
# Getting Help
15+
16+
To report a specific problem or feature request, open a new issue on Github. For questions, suggestions, or anything else, email linsir678@gmail.com.
17+
18+
19+
20+
# Author
21+
22+
关玮琳linSir - @[linsir6](https://github.com/linsir6) on GitHub, @[linsir.top](http://linsir.top)
23+
24+
# License
25+
26+
Copyright 2017 linsir
27+
28+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
29+
30+
[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
31+
32+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
33+
34+
35+
36+
# PS
37+
**已经看到这里啦,不妨给个star~**
38+
39+
![](/AndroidNote/img/background.jpg)
40+
41+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# ReactNative入门
2+
3+
## 简介
4+
5+
ReactNative旨在开发过程的高效,它具有一些原生App所不具备的优势,它的跨平台性非常强,代码的复用程度非常的高,并且可以具有热更新的能力,其次就是它的社区也在日趋的强大起来。
6+
7+
## 为什么选择ReactNative
8+
9+
纵观国内的比较大的厂商并没有完全的转移到React Native,但也没有完全的排斥说不搞React Native,所以学习这样一个新的技能对我们有百利而无一害,在学习React Native的过程中,我们也会接触到React这样一个新的UI方案,同时对一些ES6,ES7新的特性也都会有接触,总体感觉还不错~
10+
11+
## 入门
12+
13+
学习React Native最好还是要有一些React JavaScript 以及原生应用的开发经验,当然没有也无妨,慢慢一步一步的来嘛。
14+
15+
**目前我介绍的是针对Android端介绍的,后续也会把Ios相关的内容补上。**
16+
17+
需要有的软件:
18+
19+
- Node.js
20+
- Android studio
21+
- Watchman
22+
23+
运行:
24+
25+
```
26+
react-native init AwesomeProject
27+
cd AwesomeProject
28+
react-native run-android
29+
```
30+
31+
## 知识点
32+
33+
**项目的入口:** index.android.js / index.ios.js
34+
35+
**视图呈现:**
36+
37+
```
38+
render() {
39+
return (
40+
<View style={cStyles.container}>
41+
<View style={styles.row}>
42+
<Text style={styles.label}>AAA</Text>
43+
<View style={styles.rowCount}>
44+
<Text style={styles.countUnit}>BBB/Text>
45+
<Text style={styles.price}>CCC</Text>
46+
</View>
47+
</View>
48+
<LinkRow label="DDD" onPress={() => {
49+
}}/>
50+
<LinkRow label="EEE" onPress={this.logout}/>
51+
</View>
52+
);
53+
}
54+
```
55+
56+
我们的视图需要写在return()下面,并且这个下面只能有一个根View
57+
58+
**抽出公共控件:**
59+
60+
在界面中,可能我们有很多控件是可以复用的,如果我们不把它们抽出来的话,可能就会导致代码的臃肿,并且不是很利于维护,所以我们应该将公共部分抽出来。
61+
62+
**将公用的style抽象出来**
63+
64+
同上
65+
66+
## 推荐编译器
67+
68+
- 需要测试性能的时候需要使用Android studio
69+
- 与性能无关时,编写代码时还是推荐使用WebStorm编写代码
70+
71+
72+
好了,就先介绍到这里吧,在等到我有时间的时候,可以介绍一下,手写一个tab,或者实现登录注册的全部逻辑(包含网络框架)。
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# React-Native 报错记录
2+
3+
- ``xxx is not defined``
4+
5+
例如:``Component is not defined``
6+
错误原因:在当前的js文件中没有定义或者没有引入这个组建,并且有调用的地方
7+
8+
- ``JSX attributes must only assigned a non-empty expression(29:28)``
9+
10+
错误原因:这个报错就非常简单了,已经告诉我们在哪一行了,错误原因就是,定义了之后不能为空
11+
12+
- ``Cannot read property 'Aspect' of undefined``
13+
14+
错误原因:没有在当前文件夹下运行``react-native start``,导致一些关键的东西引入不进来
15+
16+
- ``undefined is not an object(evaluating 'Daily_Service.props.navigation')``
17+
18+
错误原因:一般来说,我们这样写了代码,就是因为我们认为,可以用this,或者用到某个对象的时候,但是它本身是调用不了的。
19+
20+
21+
- ``java.lang.illegalStateException:WebSocket connection no longer valid``
22+
23+
错误原因,``react-native start``命令执行之后意外中断了
24+
25+
26+
27+
28+
29+
30+
31+
- ``在View添加了onPress,但是在点击的时候,不会产生任何效果``
32+
33+
错误原因:View没有这样的方法,当遇到需要这样操作的方法的时候可以加入``<TouchableOpacity> </TouchableOpacity>``
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
File renamed without changes.
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# ReactNative短信验证码倒计时控件
2+
3+
## 功能
4+
5+
根据项目的需要,需要写一个自定义的控件,实现如下功能:
6+
7+
- 默认文字为**点击获取验证码**
8+
- 点击后出现60秒的倒计时
9+
- 颜色,字号可调
10+
- 倒计时过程中,再次点击需要忽略掉
11+
- 倒计时完成后文本恢复成**点击获取验证码**
12+
- 再几次点击同之前
13+
14+
15+
其实说了这么多,就是个最普通的验证码的功能。。。
16+
17+
## 效果
18+
19+
效果图如下:(录的图片比较一般,对付着看吧)
20+
21+
![](https://ws2.sinaimg.cn/large/006tNc79ly1fh1x98k8h0g306w01oglo.gif)
22+
23+
## 实现原理
24+
25+
自己封装了个控件,它内部含有一个Text控件,然后我们又写了一个timer,然后负责倒计时,然后每次都需要判断一下是否继续,然后加了一个flag字段,判断是否接受下次点击事件,当倒计时结束之后还需要将初始状态重置回去即可。
26+
27+
## 代码
28+
29+
### 控件代码
30+
31+
```
32+
import React, {Component } from 'react';
33+
import {
34+
StyleSheet,
35+
Text,
36+
View,
37+
Image,
38+
TextInput,
39+
TouchableHighlight,
40+
StatusBar,
41+
Alert,
42+
AppRegistry
43+
} from 'react-native';
44+
import LinkRow from '../components/LinkRow';
45+
import cStyles from '../styles/CommonStyle';
46+
47+
import axios from 'axios';
48+
49+
class MyCountTime extends Component {
50+
constructor(props) {
51+
super(props);
52+
let timeLeft = this.props.timeLeft > 0 ? this.props.timeLeft : 5;
53+
let width = this.props.width || 100;
54+
let height = this.props.height || 50;
55+
let color = this.props.color || '#42A5F5';
56+
let fontSize = this.props.fontSize || 30;
57+
let fontWeight = this.props.fontWeight || '600';
58+
let borderColor = this.props.borderColor || '#42A5F5';
59+
let borderWidth = this.props.borderWidth || 1;
60+
let borderRadius = this.props.borderRadius || 4;
61+
let backgroundColor = this.props.backgroundColor || '#42A5F5';
62+
let begin = 0;
63+
let press = this.props.press;
64+
65+
this.afterEnd = this.props.afterEnd || this._afterEnd;
66+
this.style = this.props.style;
67+
68+
this.state = {
69+
timeLeft: timeLeft,
70+
begin: begin
71+
};
72+
this.countTextStyle = {
73+
textAlign: 'center',
74+
color: '#42A5F5',
75+
fontSize: fontSize,
76+
fontWeight: fontWeight
77+
78+
};
79+
this.countViewStyle = {
80+
backgroundColor: backgroundColor,
81+
alignItems: 'center',
82+
borderColor: borderColor,
83+
borderWidth: borderWidth,
84+
borderRadius: borderRadius,
85+
width: width,
86+
height: height
87+
}
88+
}
89+
90+
countdownfn(timeLeft, callback, begin) {
91+
if (timeLeft > 0) {
92+
this.state.begin = 1;
93+
console.log("===lin===>");
94+
95+
let that = this;
96+
let interval = setInterval(function () {
97+
if (that.state.timeLeft < 1) {
98+
clearInterval(interval);
99+
callback(that)
100+
} else {
101+
let totalTime = that.state.timeLeft;
102+
that.setState({
103+
timeLeft: totalTime - 1
104+
})
105+
}
106+
}, 1000)
107+
}
108+
}
109+
110+
_beginCountDown() {
111+
if (this.state.begin === 1){
112+
return;
113+
}
114+
let time = this.state.timeLeft;
115+
console.log("===lin===> time " + time);
116+
let afterEnd = this.afterEnd;
117+
let begin = this.state.begin;
118+
console.log("===lin===> start " + begin);
119+
this.countdownfn(time, afterEnd, begin)
120+
}
121+
122+
_afterEnd(that) {
123+
console.log('------------time over');
124+
that.setState({
125+
begin : 0,
126+
timeLeft : 5,
127+
})
128+
}
129+
130+
componentDidMount() {
131+
132+
}
133+
134+
render() {
135+
return (
136+
<View style={{position:'absolute',top:13,right:43,height:30}}>
137+
<Text
138+
onPress={this._beginCountDown.bind(this)}
139+
style={{color: '#42A5F5', fontSize: 17,height:40 , zIndex:999}}> { this.state.begin === 0 ? '点击获取验证码' : this.state.timeLeft} </Text>
140+
141+
</View>
142+
)
143+
}
144+
}
145+
146+
```
147+
148+
### 应用代码
149+
150+
```
151+
<MyCountTime timeLeft={5}>
152+
153+
</MyCountTime>
154+
155+
```
156+
157+
158+
当然这只是,最简单的应用的代码,我们还提供了很多的自定义的属性,大家可以根据自己项目的需要,去调节这些参数。
159+
160+
----
161+
162+
由于最近刚开始认真的搞RN,可能有一些封装的不是最佳实践,还是希望大家多提意见,和大家一起进步吧。
163+
164+

0 commit comments

Comments
 (0)