kind of stuck on this little tidbit. I'm pretty new to redux so I'm just trying to figure it out. I'm trying to seperate my players array which come in on the response based on the players position. It all works except that the state is just constantly changing to the most recent player in that position instead of adding to the array of players in that position. Here are my actions and reducer:
//action.js
export const FETCH_PLAYERS_SUCCESS = 'FETCH_PLAYERS_SUCCESS';
export const fetchPlayersSuccess = players => ({
type: FETCH_PLAYERS_SUCCESS,
players
});
export const SET_WR = 'SET_WR';
export const setWR = wr => ({
type: SET_WR,
wr
});
export const SET_QB = 'SET_QB';
export const setQB = qb => ({
type: SET_QB,
qb
});
export const SET_RB = 'SET_RB';
export const setRB = rb => ({
type: SET_RB,
rb
});
export const SET_TE = 'SET_TE';
export const setTE = te => ({
type: SET_TE,
te
});
export const fetchPlayers = () => {
return dispatch => {
const proxyurl = "https://cors-anywhere.herokuapp.com/";
const url = "http://api.fantasy.nfl.com/v1/players/stats?statType=seasonStats&season=2017&week=1&format=json"; // site that doesn’t send Access-Control-*
fetch(proxyurl + url)
.then(res => res.json())
.catch(error => {
console.error('Error:', error)
dispatch(fetchPlayersError(error))
})
.then(response => {
let thisPlayer = response.players
for(let i=0; i<thisPlayer.length; i++){
if(thisPlayer[i].position == 'WR'){
dispatch(setWR(thisPlayer[i]))
}
if(thisPlayer[i].position == 'QB'){
dispatch(setQB(thisPlayer[i]))
}
if(thisPlayer[i].position == 'RB'){
dispatch(setRB(thisPlayer[i]))
}
if(thisPlayer[i].position == 'TE'){
dispatch(setTE(thisPlayer[i]))
}
}
dispatch(fetchPlayersSuccess(response))
});
}}
my reducer:
//reducer.js
const initialState = {
players: [],
loading: false,
error: null,
wr: [],
qb: [],
rb: [],
te: []
};
export default (state = initialState, action) => {
if (action.type === FETCH_PLAYERS_REQUEST) {
return Object.assign({}, state, {
loading: true,
error: null
});
}
if (action.type === FETCH_PLAYERS_SUCCESS) {
//console.log(state, action);
return Object.assign({}, state, {
players: action.players,
loading: false
});
}
if (action.type === SET_QB) {
//console.log(state, action);
return Object.assign({}, state, {
qb: action.qb,
loading: false
});
}
if (action.type === SET_RB) {
//console.log(state, action);
return Object.assign({}, state, {
rb: action.rb,
loading: false
});
}
if (action.type === SET_WR) {
//console.log(state, action);
return Object.assign({}, state, {
wr: action.wr,
loading: false
});
}
if (action.type === SET_TE) {
//console.log(state, action);
return Object.assign({}, state, {
te: action.te,
loading: false
});
}
if (action.type === FETCH_PLAYERS_ERROR) {
return Object.assign({}, state, {
loading: false,
error: action.error
});
}
return state;
};
thanks for any help in advance.
SET_TEis the array of players? Should it add to existingtearray? If soObject.assign({}, state, { te: action.te, loading: false });will overwrite previous state forte