React Native基础&入门教程:以一个To Do List小例子,看props和state

前端开发 作者: 2024-08-22 01:35:01
本文由葡萄城技术团队于博客园原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 在上篇中,我们介绍了什么是Flexbox布局,以及如何使用Flexb
  • 第一,“从应用开始到结束”,意味着它在时间上有一段生命周期(Life Cycle)
  • 第二,应用其实可以拥有很多种状态(State),比如,正常时是一种状态,出错时是另一种状态。而且这些状态能够在某些条件下进行转换。

基本概念:

开始尝试:

export default class ToDoListAdd extends Component<Props> {
    constructor(props) {
        super(props);
    }
    onPress() { } // 暂且为空

    render() {
        return (
            <View style={styles.container}>
                <View style={styles.header}>
                    <Text style={styles.add} onPress={this.props.onBack}>返回</Text>
                </View>
                <View style={styles.body}>
                    <TextInput />
                </View>
                <View style={styles.footer}>
                    <Button title="确定" onPress={this.onPress} style={styles.btn} />
                </View>
            </View>
        );
    }
}
export default class ToDoListmain extends Component<Props> {
    constructor(props) {
        super(props);
        this.state = {
            isEditing: false
        };

        this.onEdit = this.onEdit.bind(this);
        this.renderItem = this.renderItem.bind(this);
    }

    renderFooter(toggleCheckAll,isAllChecked) {
        if (!this.state.isEditing) {
            return null;
        }
        const count = this.props.todoList.filter(item => item.isChecked).length;
        return (
            <View style={styles.footer}>
                <CheckBox onValueChange={toggleCheckAll} value={isAllChecked} />
                <Text style={styles.menu}>{`已选择:${count}项`}</Text>
            </View>
        );
    }

    onEdit() {
        this.setState((prevState) => {
            return {
                isEditing: !prevState.isEditing
            }
        });
    }

    renderItem(item) {
        return (<ToDoListItem {...item}
            toggleItemCheck={this.props.toggleItemCheck}
            isEditing={this.state.isEditing} />);
    }

    render() {
        const { toggleCheckAll,isAllChecked,onAddItem,todoList } = this.props;

        return (
            <View style={styles.container}>
                <View style={styles.header}>
                    <Text style={styles.add} onPress={onAddItem}>添加</Text>
                    <Text style={styles.title}>待办列表</Text>
                    <Text style={styles.multi}
                        onPress={this.onEdit}>{this.state.isEditing ? '取消' : '多选'}
                    </Text>
                </View>

                <FlatList style={styles.body} isEditing={this.state.isEditing}
                    data={todoList} renderItem={this.renderItem} />

                {this.renderFooter(toggleCheckAll,isAllChecked)}
            </View>
        );
    }
}
export default class ToDoListItem extends Component<Props> {
    constructor(props) {
        super(props);
    }

    render() {
        const { toggleItemCheck,item } = this.props;
        const { isChecked,detail,level } = item;
        const basicLevelStyle = styles.level;
        let specificLevelStyle;
        if (level === 'info') {
            specificLevelStyle = styles.info;
        } else if (level === 'warning') {
            specificLevelStyle = styles.warning;
        } else {
            specificLevelStyle = styles.error;
        }

        return (
            <View style={styles.container}>
                {this.props.isEditing && <CheckBox onValueChange={(value) => toggleItemCheck(item,value)} style={styles.checkbox} value={isChecked} />}
                <Text style={styles.detail}>{detail}</Text>
                <View style={[basicLevelStyle,specificLevelStyle]}></View>
            </View>
        );
    }
}
import ToDoListMain from './ToDoListMain';
import ToDoListAdd from './ToDoListAdd';
export default class App extends Component<Props> {
    constructor(props) {
        super(props);
        this.state = {
            current: 'main',todoList: [
                {
                    level: 'info',detail: '一般的任务',isChecked: false,key: '0'
                },{
                    level: 'warning',detail: '较重要的任务',},{
                    level: 'error',detail: '非常重要且紧急的任务',key: '2'
                }
            ]
        }
        this.onAddItem = this.onAddItem.bind(this);
        this.onBack = this.onBack.bind(this);
        this.toggleCheckAll = this.toggleCheckAll.bind(this);
        this.toggleItemCheck = this.toggleItemCheck.bind(this);
    }

    onAddItem() {
        this.setState((prevState) => {
            return {
                current: 'add'
            }
        });
    }

    onBack() {
        this.setState({
            current: 'main'
        });
    }

    toggleCheckAll() {
        const flag = this.isAllChecked();
        const newTodos = this.state.todoList.map(item => {
            return {
                ...item,isChecked: !flag
            };
        });
        this.setState({
            todoList: newTodos
        });
    }

    toggleItemCheck(item,key) {
        const newTodos = this.state.todoList.map(todo => {
            if (todo !== item) {
                return todo;
            }
            return {
                ...todo,isChecked: !item.isChecked
            }
        });
        this.setState({
            todoList: newTodos
        });
    }

    isAllChecked() {
        if (!this.state.todoList) return false;
        if (this.state.todoList.length === 0) return false;
        return this.state.todoList.every(item => item.isChecked);
    }

    render() {
        if (this.state.current === 'main') {
            return (<ToDoListMain
                isAllChecked={this.isAllChecked()}
                toggleCheckAll={this.toggleCheckAll}
                toggleItemCheck={this.toggleItemCheck}
                onAddItem={this.onAddItem}
                todoList={this.state.todoList} />);
        } else {
            return (<ToDoListAdd onBack={this.onBack} />);
        }
    }
}
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_66404.html