สวัสดี ฉันกำลังพยายามเรียนรู้ 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);
this.props.arr1
มาจากไหน? - person Carlos C   schedule 30.12.2017mapDispatchToProps
ไปยังconnect()
มันควรจะเป็นmapStateToProps
- person Adam Kipnis   schedule 30.12.2017