reactjs - Where I handle API calls in react-native and redux -


i'm building android app using react-native + redux + immutable

i structured app this

/src  -/components  -/actions (where have action types , actions)  -/reducers  -/api (i'm not sure if i'm doing right) 

ok, have action need make api call fetch this:

import * types './casestypes'  export function getcases() {     return {       type: types.get_cases     } } 

this cases reducer:

const initialstate = fromjs({   isloading: false,   cases: null }) export default function cases(state = initialstate, action = {}) {      switch(action.type) {          case types.request_cases:           return state.set('isloading', true)          case types.recive           return state.set('cases', //set here array recive api call)          default:           return state     } } 

so, thing dont understand, should make api call in reducer can merge initial state recive api call?

your app structure sound.

i recommand usage of redux-thunk handle dispatch. here how work in case:

let's have 'cases' part of state tree. put api call in action suggested fetch new cases:

import * types './casestypes'  export function getcases() {     return fetch(...)              ...              .then((json) => {                dispatch({                           type: types.received_cases,                  isloading: false,                  cases: json,                } } 

and in reducer newly dispatched action merge new cases state tree:

const initialstate = fromjs({   isloading: false,   cases: null })  export default function cases(state = initialstate, action = {}) {      switch(action.type) {          case types.request_cases:           return state.set('isloading', true)          case types.received_cases:           return object.assign({}, state, {             cases: state.cases.merge(action.cases),           });          default:           return state     } } 

i working structure , works pretty well. hope helps!


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -