ข้อมูลการดึงข้อมูลว่างเปล่าใน ComponentwillMount ใน Redux แบบเนทีฟแบบตอบสนอง

สวัสดี ฉันกำลังพยายามเรียนรู้ react-native จากหลักสูตร react-native ของ Stephen Grider ฉันติดอยู่กับการโหลดข้อมูลจากบริการเว็บของฉันและแสดงรายการโดยใช้ redux และ lodash ฉันสามารถรับข้อมูลได้สำเร็จและสามารถดูได้ในการเรนเดอร์ (console log) และแต่อุปกรณ์ประกอบฉากของฉันจะเป็นโมฆะเสมอในcomponentDidUpdateหรือcomponentWillMount ความช่วยเหลือใด ๆ ที่ชื่นชมขอบคุณ ตัวลดจะเป็นแบบนี้

    import { TEST_FETCH, TEST_LOAD } from "../actions/types";

const INITIAL_STATE = { dataSource: [] };

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case TEST_FETCH:
      return { ...state, loading: false, dataSource: action.payload.data };
    case TEST_LOAD:
      return { ...state, loading: true, error: "" };
    default:
      return state;
  }
};

และการกระทำคือ ;

    import { TEST_LOAD, TEST_FETCH } from "./types";
export  const  getdata = ( ) => {
  return  dispatch => {
    dispatch({ type: TEST_LOAD });
     fetch("http://myserver/getdata", {
      method: "GET",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
      }
    })
      .then(response => {
        return response.json();
      })
      .then(responseData => {
        return responseData;
      })

      .then(data => {
        // return data;
        dispatch({ type: TEST_FETCH, payload: data });
      });
  };
};

และหน้าคือ ;

       import _ from 'lodash';
    import React, { Component } from "react";
    import { View, Text, ListView } from "react-native";
    import { connect } from "react-redux";
    import { getdata } from "../actions";

    class testList extends Component {
      componentWillMount() {
       this.props.getdata();
      }
      componentDidMount() {
       console.log(this.props.myarray ); // myarray is empty
        this.createDataSource(this.props.myarray);
      }

      componentDidUpdate() {
       console.log(this.props.myarray ); // I tried this but still myarray is empty
        this.createDataSource(this.props.myarray);
      }
      createDataSource({ dtsource }) {
// sure dtsource is null too
         const ds = new ListView.DataSource({
          rowHasChanged: (r1, r2) => r1 !== r2});
         this.dataSource = ds.cloneWithRows(dtsource);
      }
      render() {
    console.log(this.props.myarray); // if I write here,I can see my json's output
        return <View>
            <Text>Employee List</Text>
            <ListView dataSource={this.props.myarray} renderRow={rowData => <Text
                >
                  {rowData}
                </Text>} />
          </View>;
      }
    }

    const mapStateToProps = state => {
      const myarray= _.map(state.testForm.dataSource, function(v) {
        return { ...v };
      });
          return { myarray};
    };
    export default connect(mapStateToProps , { getdata })(testList);

person slayer35    schedule 30.12.2017    source แหล่งที่มา
comment
คุณควรส่งการกระทำของคุณจากcomponentDidMount ดู reactjs.org/docs/react-component.html#componentwillmount   -  person Carlos C    schedule 30.12.2017
comment
this.props.arr1มาจากไหน?   -  person Carlos C    schedule 30.12.2017
comment
@CarlosC ขออภัย ฉันลืมเปลี่ยนในขณะที่เขียนที่นี่ มันคือ myarray   -  person slayer35    schedule 30.12.2017
comment
คุณกำลังส่งผ่าน mapDispatchToProps ไปยัง connect() มันควรจะเป็น mapStateToProps   -  person Adam Kipnis    schedule 30.12.2017
comment
@ AdamKipnis ฉันแก้ไขแล้ว แต่ฉันเขียนผิดที่นี่ขออภัย   -  person slayer35    schedule 30.12.2017
comment
@CarlosC ถ้าคุณหมายถึงสิ่งนี้ ฉันพยายามที่จะรู้แต่ยังคงเห็น myarray มันยังว่างเปล่า componentDidMount() { this.props.getdata(); }   -  person slayer35    schedule 30.12.2017
comment
คุณเพิ่งส่งอาร์เรย์ไปยังแหล่งข้อมูลซึ่งไม่ใช่สิ่งที่คาดหวัง คาดว่าจะมี ListViewDataSource ดู facebook.github.io/react-native/docs/listview.html #แหล่งข้อมูล   -  person Carlos C    schedule 30.12.2017


คำตอบ (1)


ฉันอยากจะแนะนำให้คุณใช้ FlatList เนื่องจาก ListView เลิกใช้แล้วและมีประสิทธิภาพไม่ดี ในระหว่างนี้ คุณสามารถใช้ข้อมูลโค้ดด้านล่างเพื่อส่งออบเจ็กต์ที่ถูกต้องไปยังแหล่งข้อมูลได้ คุณอาจต้องเพิ่มการตรวจสอบ null ขึ้นอยู่กับสถานะของข้อมูลที่คุณส่งไปยัง myarray

import _ from 'lodash';
import React, { Component } from "react";
import { View, Text, ListView } from "react-native";
import { connect } from "react-redux";
import { getdata } from "../actions";

class testList extends Component {

  constructor(props) {      
    super(props);               
    this.state = {      
    dataSource: new ListView.DataSource({       
      rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows(props.myarray ? props.myarray : [])      
    };      
  }

  componentDidMount() {
    this.props.getdata();
  }

  componentDidUpdate() {
    this.setState({     
       dataSource: this.state.dataSource.cloneWithRows(props.myarray ? props.myarray : [])      
    });
  }

  render() {
    return <View>
        <Text>Employee List</Text>
        <ListView dataSource={this.props.myarray} renderRow={rowData => <Text
            >
              {rowData}
            </Text>} />
      </View>;
  }
}

const mapStateToProps = state => {
  const myarray =  _.map(state.testForm.dataSource, function(v) {
    return { ...v };
  });
      return { myarray};
};
export default connect(mapStateToProps , { getdata })(testList);
person Carlos C    schedule 30.12.2017
comment
ขอบคุณสำหรับความคิดเห็นของคุณ ฉันลองแล้ว แต่มีข้อผิดพลาด TypeError: Cannot Convert undef or null to object บางทีปัญหาของฉันอาจแตกต่างออกไป ฉันยังใหม่มากที่จะโต้ตอบ ดังนั้นจึงยังไม่รู้เรื่องพื้นฐานบางอย่างที่ฉันเดา โดยวิธีที่ฉันเห็นข้อมูล json ของฉันใน mapStateToProps ; console.log (state.testForm.dataSource) - person slayer35; 31.12.2017
comment
ฉันอัปเดตเพื่อทำการตรวจสอบเป็นโมฆะ/ไม่ได้กำหนด ฉันพูดถึงในความคิดเห็นของฉันว่าจะทำอะไรแบบนั้นหากจำเป็นโดยพิจารณาจากสถานะของข้อมูลของคุณ - person Carlos C; 31.12.2017