Answer the question
In order to leave comments, you need to log in
How to get data with api with one action and reducer in React + Redux?
Came from Angular 2 to React + Redux.
In Angular, he described base.service, which implemented all the basic methods for working with data.
And then the components were simply typed as a constructor, referring to this base.service.
How to implement similar generic base.reducer and base.action in Redux?
I'm sure there are examples, but I can't find them.
To begin with, I try to pull data (ex. users, hospitals) from api using one reducer and one action, calling the fetchData (url) action. But then I don’t understand how to save the data in the store with the reducer, corresponding to the response from the api. To be store.users and store.hospitals.
Answer the question
In order to leave comments, you need to log in
maybe something like this..
const myState = {
users: [],
hospitals: []
}
const myReducer = (state = myState, action) => {
if( action.type === 'FETCH_DATA' ) {
return myState[action.storeType].concat(data)
}
return state
}
const fetchData = (url) => {
fetch(...).then(data => {
return {
type: "FETCH_DATA",
data: data.result,
storeType: data.type
}
})
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question