簡體   English   中英

如何在本機反應中刷新ListView Component?

[英]How refresh the ListView Component in react native?

我試圖創建一個待辦事項列表,然后試圖使用此復選框包

反應母語 - 復選框

我不知道這個問題是否來自那個包裹 在此處輸入圖片說明

當我單擊復選框並單擊刪除按鈕時,會發生這種情況

在此處輸入圖片說明 這是我用於顯示列表的代碼

    interface TodoProps{
        todo: TodoModel;
        ter:string;
    }
    interface TodoState{
        dataSource: any;
        myTodo: Array<ITodo>;
    }

    const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => true});

    export default class Todo extends React.Component <TodoProps,TodoState>{

        constructor(props:TodoProps) {
            super(props);

            this.state = {
                dataSource: ds.cloneWithRows([]), // or put your initial data here
                myTodo: []
            };
        }

  componentWillReceiveProps = (nextProps) => {
           console.log(this.state.myTodo);
        let data = {
            title: nextProps.ter,
            isChecked: false
        };
        let todoList = this.state.myTodo;

        if (nextProps.ter) {
            todoList.push(data);
        }

        this.setState({
            myTodo: todoList
        });

    }

        onDelete(){
            let todos = this.state.myTodo.filter((todo:ITodo) => {
                 return !todo.isChecked;
             });

            this.setState({
                myTodo: todos
            });
        }

        render() {

            let dataSource = this.state.dataSource.cloneWithRows(this.state.myTodo);
            return (
                <View style={styles.container}>
                    <View style={styles.box2}>
                        <ListView enableEmptySections={true} dataSource={dataSource} renderRow={(rowData, sectionID, rowID) => <TodoList data={rowData} onClick={this.onClick.bind(this)} id={rowID} /> } />
                    </View>
                    <View style={styles.box3}>
                        <TouchableOpacity style={styles.bbox1} onPress={()=> alert('weee')}>
                            <Text style={styles.tabText}>All</Text>
                        </TouchableOpacity>
                        <TouchableOpacity style={styles.bbox2} onPress={()=> alert('weee')}>
                            <Text style={styles.tabText}>Complete</Text>
                        </TouchableOpacity>
                        <TouchableOpacity style={styles.bbox3} onPress={()=> alert('weee')}>
                            <Text style={styles.tabText}>InComplete</Text>
                        </TouchableOpacity>
                        <TouchableOpacity style={styles.bbox4} onPress={()=> this.onDestroy()}>
                            <Text style={styles.tabText}>Delete</Text>
                        </TouchableOpacity>
                    </View>
                </View>
            )
        }
    }

但是如果我控制台記錄數據。 我發現所有isChecke:都為false。 僅復選框視圖不起作用。 我是否需要為此使用componentwillamount()?

使用以下示例代碼

 _onRefresh() {
    this.setState({refreshing: true});
    // your callback function or call this.componentDidMount()
  }

  render() {
    return (
      <ListView
        refreshControl={
          <RefreshControl
            refreshing={this.state.refreshing}
            onRefresh={this._onRefresh.bind(this)}
          />
        }
        ...
      >
      ...
      </ListView>
    );
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM