2017-12-30 42 views
0

こんにちは私は、スティーブングリッドの反応ネイティブコースから反応したネイティブを覚えようとしています。私は自分のウェブサービスからデータを読み込み、レビュックスとロダッシュを使ってリストします。データを正常に取得し、render(console.log)でそれを見ることができますが、私の小道具は常にcomponentDidUpdateまたはcomponentWillMountでnullです。 ご協力いただきありがとうございます。 減速機はこのようです。フェッチデータがコンポーネントウィルマウントで空です。ネイティブレスキュー

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); 
+0

componentDidMountからアクションを送信する必要があります。 https://reactjs.org/docs/react-component.html#componentwillmountをご覧ください。 –

+0

'this.props.arr1'はどこから来ますか? –

+0

@CarlosC申し訳ありませんが、ここに書いている間に変更するのを忘れます。これはmyarrayです – slayer35

答えて

1

ListViewは廃止され、パフォーマンスが悪いので、FlatListを使用することをお勧めします。その間、以下のコードスニペットを使用して、正しいオブジェクトをdataSourceに渡すことができます。 myarrayに渡すデータの状態に応じて、nullのチェックを追加する必要があります。

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); 
+0

thxを参照してください。試しましたが、エラーが発生しました。 "TypeError:未定義またはnullをオブジェクトに変換できません"。たぶん私の問題は何か違う、私は非常に非常に反応する新しいので、私は推測するいくつかの基本的なものを知っていない。私が見ることができるように、mapStateToPropsの私のjsonデータ。 console.log(state.testForm.dataSource) – slayer35

+0

null/undefinedチェックを行うように更新しました。私はあなたのデータの状態に基づいて、必要に応じてそのようなことをするコメントに言及しました。 –