【问题标题】:ListView not re-rander after update or edite data react nativeListView 在更新或编辑数据后不会重新渲染本机反应
【发布时间】:2017-06-09 03:42:06
【问题描述】:

更新帖子

Khatri 我认为你是对的,所以我更改了我的代码但我仍然​​不明白如何在更新我的待办事项文本后重新排列我的列表。

我制作了一个 Todo 应用程序。一切正常。但是当我要更改我的待办事项文本时,它会成功更改数据库,但是如果不重新打开我的应用程序,我就看不到我的更新文本或待办事项。 更改文本后如何重新渲染?

        const myFirebaseRef = Firebase.database().ref();
        this.itemsRef = myFirebaseRef.child('items');

        this.state = {
            newTodo: '',
            todoSource: new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2}),
            modalVisible: false,
            editTodoVal: 'Edit Your Todo',
            updateId: '',
            refreshing: false,
        };
        this.items = [];


componentDidMount() {
        this.itemsRef.on('child_added', (dataSnapshot) => {
            this.items.push({id: dataSnapshot.key, text: dataSnapshot.val()});
            this.setState({
                todoSource: this.state.todoSource.cloneWithRows(this.items)
            });
        });

        this.itemsRef.on('child_removed', (dataSnapshot) => {
      
            this.items = this.items.filter((x) => x.id !== dataSnapshot.key);
            this.setState({
                todoSource: this.state.todoSource.cloneWithRows(this.items)
            });
        });
        this.itemsRef.on('child_changed', (dataSnapshot) => {
            // what code hare I can make for it ???
        });

    }


addTodo() {
    if (this.state.newTodo !== '') {
        console.log('ADD Todo Successfully');
        this.itemsRef.push({
            todo: this.state.newTodo
        });
        this.setState({
            newTodo: ''
        });
    }
}

updateTodo() {
    if (this.state.editTodoVal !== '') {
        this.itemsRef.child(this.state.updateId)
            .update({
                todo: this.state.editTodoVal
            });
        alert('Success fully update Todo');
        this.setState(this.state);
        this.setState({
            editTodoVal: ''
        });
        this.setModalVisible(false)
    }
}

removeTodo(rowDate) {
    this.itemsRef.child(rowDate.id).remove();
}

 editTodo(rowDate) {
    this.setState({
        editTodoVal: rowDate.text.todo,
        updateId: rowDate.id
    });

    this.setModalVisible(true)
}

            <ListView
                dataSource={this.state.todoSource}
                renderRow={this.renderRow.bind(this)}/>

renderRow(rowData) {
        return (
            <View style={styles.appContainerBody}>
                <View>
                    <Text style={styles.todoText}>{rowData.text.todo}</Text>
                </View>
                <View style={styles.actionView}>
                    <Text onPress={() => this.removeTodo(rowData)} style={styles.removedText}>
                        <FontAwesome name="trash" size={16}/>
                    </Text>
                    <Text onPress={() => this.editTodo(rowData)} style={styles.editeText}>
                        <FontAwesome name="pencil-square-o" size={16}/>
                    </Text>
                </View>
            </View>
        )
    }

【问题讨论】:

    标签: reactjs listview react-native


    【解决方案1】:

    它不会重新渲染您的应用程序,因为您正在直接改变状态,这是错误的做法。您应该使用 setState 方法来更新状态,这也会在状态更改成功后触发重新渲染

    editTodo(rowDate) {
        this.setState({
             editTodoVal: rowDate.text.todo, 
             updateId:  rowDate.id
        });
    
        this.setModalVisible(true)
    }
    

    【讨论】:

    • #khtri 感谢您的建议,但我仍然没有得到如何在更改值或待办事项后重新排序的解决方案。我已经应用了你的代码
    • 好的,你如何渲染现有的待办事项。您还需要从渲染位置更新数组
    • 查看如何更新 todo:stackoverflow.com/questions/44404200/…
    • #Khatri 再次感谢。其实我是 react-native 的新手。我已经尝试了 4 个多小时,但我不明白你的链接可以,你能帮忙说明必须添加哪一行或代码。如果你有时间。:)
    • 您应该在 editTodo 函数本身中执行此操作。
    猜你喜欢
    • 2017-12-13
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多