Ich habe Probleme, zurück 1 Array-Element in den Ergebnissen von einem API-Anruf zurückgegeben.ReactJS, Redux: Wie gebe ich ein Array von Elementen in meinem STATE zurück?
Dies ist die Funktion in Aktionen.
followCheckList:function(userId){
for(var i= 0; i < userId.length;i++){
return{
types: [C.FOLLOW_CHECK_LIST, C.FOLLOW_CHECK_LIST_SUCCESS, C.FOLLOW_CHECK_LIST_FAIL],
promise: (client)=> client.get(`/graph/me/follows/${userId[i]}`)
.then(result => {
return result.id;
})
}
}
}
Meine Minderer
case C.FOLLOW_CHECK_LIST:
return{
...state,
follow_check_list:null,
error:false,
};
case C.FOLLOW_CHECK_LIST_SUCCESS:
return{
...state,
//break here
error:false,
follow_check_list: action.result
};
case C.FOLLOW_CHECK_LIST_FAIL:
return{
...state,
follow_check_list:false,
error:action.error,
};
default: return state || {loading: false,
loaded: null,
list_loaded: null};
}
Ich bin 2 Ergebnisse von follow_check_list : action.result
so in meinem Zustand erwarten sollte ich
follow_check_list : Array[2].
0: item1,
1: item2
Aber stattdessen sehe ich sehen:
follow_check_list : Array[1].
0: item1,
Update Meine Komponente. arrayIds hat 2 Elemente.
let arrayIds=[];
const FollowStatus = React.createClass ({
componentWillMount(){
arrayIds.push(this.props.status);
},
componentDidMount(){
this.props.followCheckList(arrayIds)
},
Vielen Dank im Voraus Jungs
Woher soll "followers_checklist" kommen? – markerikson
ist innerhalb der Funktion. Wenn es undefiniert ist, dann erstelle ich es. Ich habe dies getan, um zu verhindern, dass die Variable ein zweites Mal instanziiert wird, wenn die Funktion mehr als einmal aufgerufen wird. – joejoeso
Und wie sieht Ihr "Ergebnis" im AJAX-Callback aus? Ist es ein einzelnes Objekt? Weil Sie nur das eine Objekt in das Array schieben ... – markerikson