RN TODO代码解析之二
2017-12-08 15:42
309 查看
在第二篇里我们用flatlist改写之前的版本,并且使用es6的箭头函数实现列表项的渲染。
import React, {
Component,
PropTypes
} from 'react'
import {
AppRegistry,
StyleSheet,
View,
Text,
Image,
Button,
TextInput,
ListView,
Switch,
TouchableOpacity,
FlatList
} from 'react-native'
class TodoApp extends Component {
constructor(props) {
super(props);
// Set the initial state.
this.state = {
onlyShowNotDone: false,
todos: [
this.makeTodo(3),
this.makeTodo(2, true),
this.makeTodo(1)
],
}
}
addTodo() {
// Inject a new todo at the start of the list.
this.setState({
todos: [this.makeTodo(), ...this.state.todos]
});
}
makeTodo(number, done) {
const id = number ? number : this.state.todos.length + 1;
return {
id: id,
done: done ? true : false,
text: 'Todo Item #' + id,
key: id
}
}
toggleTodo(todo) {
let newTodo = { ...todo };
newTodo.done = !todo.done;
const index = this.state.todos.indexOf(todo);
// Recreate a list but replace the existing one.
let todos = [...this.state.todos.slice(0, index), newTodo, ...this.state.todos.slice(index + 1)];
this.setState({
todos: todos
});
}
render() {
const todos = this.state.todos.filter((todo) => {
if (this.state.onlyShowNotDone) {
return !todo.done;
}
return true;
});
return (
<View style={styles.container}>
<View style={styles.options}>
<TouchableOpacity onPress={() => this.addTodo()} style={styles.add}>
<Text>+ Add a todo</Text>
</TouchableOpacity>
<View style={styles.hide}>
<Text>Hide done</Text>
<Switch
onValueChange={(value) => this.setState({ onlyShowNotDone: value })}
value={this.state.onlyShowNotDone} />
</View>
</View>
<FlatList
data={todos}
renderItem={
({ item, index }) => (
<View style={styles.todo} key={item.id}>
<View>
<Switch onValueChange={() => this.toggleTodo(item)} value={item.done} />
</View>
<View>
<Text>{item.text}</Text>
</View>
</View>
)}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
add: {
flex: 1,
padding: 10
},
hide: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-around'
},
options: {
flexDirection: 'row',
marginTop: 50,
marginBottom: 30
},
todo: {
flex: 1,
flexDirection: 'row',
marginBottom: 10
}
});
export default class App extends React.Component {
render() {
return <TodoApp />;
}
}
Todo demo系列之第二版,改动点:
a.列表改成了flatlist, 直接使用数据源todos就可以了,无需自己创建和填充专门的datasource,这种使用方式非常接近原生的tableview。
需要注意的是: 1.绑定的renderItem方法的参数名必须是item, index, 这里的item不能换成其他字符串! 2.todos里面的项需要有一个key(可以和id相同),否则会告警。
b.将renderItem方法直接使用箭头函数来实现了,移除了此前单独的渲染方法。
import React, {
Component,
PropTypes
} from 'react'
import {
AppRegistry,
StyleSheet,
View,
Text,
Image,
Button,
TextInput,
ListView,
Switch,
TouchableOpacity,
FlatList
} from 'react-native'
class TodoApp extends Component {
constructor(props) {
super(props);
// Set the initial state.
this.state = {
onlyShowNotDone: false,
todos: [
this.makeTodo(3),
this.makeTodo(2, true),
this.makeTodo(1)
],
}
}
addTodo() {
// Inject a new todo at the start of the list.
this.setState({
todos: [this.makeTodo(), ...this.state.todos]
});
}
makeTodo(number, done) {
const id = number ? number : this.state.todos.length + 1;
return {
id: id,
done: done ? true : false,
text: 'Todo Item #' + id,
key: id
}
}
toggleTodo(todo) {
let newTodo = { ...todo };
newTodo.done = !todo.done;
const index = this.state.todos.indexOf(todo);
// Recreate a list but replace the existing one.
let todos = [...this.state.todos.slice(0, index), newTodo, ...this.state.todos.slice(index + 1)];
this.setState({
todos: todos
});
}
render() {
const todos = this.state.todos.filter((todo) => {
if (this.state.onlyShowNotDone) {
return !todo.done;
}
return true;
});
return (
<View style={styles.container}>
<View style={styles.options}>
<TouchableOpacity onPress={() => this.addTodo()} style={styles.add}>
<Text>+ Add a todo</Text>
</TouchableOpacity>
<View style={styles.hide}>
<Text>Hide done</Text>
<Switch
onValueChange={(value) => this.setState({ onlyShowNotDone: value })}
value={this.state.onlyShowNotDone} />
</View>
</View>
<FlatList
data={todos}
renderItem={
({ item, index }) => (
<View style={styles.todo} key={item.id}>
<View>
<Switch onValueChange={() => this.toggleTodo(item)} value={item.done} />
</View>
<View>
<Text>{item.text}</Text>
</View>
</View>
)}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
add: {
flex: 1,
padding: 10
},
hide: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-around'
},
options: {
flexDirection: 'row',
marginTop: 50,
marginBottom: 30
},
todo: {
flex: 1,
flexDirection: 'row',
marginBottom: 10
}
});
export default class App extends React.Component {
render() {
return <TodoApp />;
}
}
Todo demo系列之第二版,改动点:
a.列表改成了flatlist, 直接使用数据源todos就可以了,无需自己创建和填充专门的datasource,这种使用方式非常接近原生的tableview。
需要注意的是: 1.绑定的renderItem方法的参数名必须是item, index, 这里的item不能换成其他字符串! 2.todos里面的项需要有一个key(可以和id相同),否则会告警。
b.将renderItem方法直接使用箭头函数来实现了,移除了此前单独的渲染方法。
相关文章推荐
- RN TODO代码解析之一
- RN TODO代码解析之三 (WithMobX!)
- FrameLayout measure过程源码Log全解析之二:修改framework代码,输出Log
- 【dubbo源码解读系列】之二 dubbo代码启动入口解析(自定义main方法)
- java二维码生成与解析代码实现
- 二维码生成与解析代码实现
- Hpple-iOS解析HTML-hpple读取html文件内容的代码 更新了下拉刷新功能
- javascript图片轮播代码的一点解析
- Smarty模板Windows下写代码 放到CentOS6.5无法正确解析
- Ryu代码解析(一)
- 好代码、坏代码之二
- 卷积神经网络(CNN)代码实现(MNIST)解析
- mdadm代码解析
- C# IFF图形结构解析代码
- 深圳俱乐部10月活动《代码解析Castle(IOC)应用实例 -开源CMS 系统Cuyahoga》
- HDFS1.0源代码解析—DataNode数据接收线程DataXceiverServer与DataXceiver解析
- Android json数据解析详解及实例代码
- 解析Android开发优化之:从代码角度进行优化的技巧
- 带负数的数列,求解和最大的相邻子序列之二:代码清晰
- 【Python】HMM 代码解析理解