我在 react-native 和 redux 中处理 API 调用的地方
Where I handle API calls in react-native and redux
我正在使用 react-native
+ redux
+ immutable
构建一个 Android 应用
我的应用程序结构如下
/src
-/components
-/actions (where I have action types and actions)
-/reducers
-/api (I'm not sure if I'm doing this right)
好的,所以我有一个动作,我需要像这样用 fetch 进行 API 调用:
import * as types from './casesTypes'
export function getCases() {
return {
type: types.GET_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 the array that I recive from api call)
default:
return state
}
}
所以,问题是我真的不明白,我应该在哪里进行 API 调用,以便在 reducer 中我可以将我的初始状态与我从 API 调用中收到的内容合并?
您应该尝试 redux-thunk or redux-saga,它们是 redux 中间件,用于处理异步操作,例如进行 API 调用。
查看这个使用了 redux-thunk 的项目:sound-redux-native
您的应用程序结构合理。
我还推荐使用 redux-thunk 来处理调度。以下是它在您的情况下的工作方式:
假设您有 'cases' 作为状态树的一部分。
我会按照您建议的方式在您的操作中调用 API 以获取新案例:
import * as types from './casesTypes'
export function getCases() {
return fetch(...)
...
.then((json) => {
dispatch({
type: types.RECEIVED_CASES,
isLoading: false,
cases: json,
}
}
现在在你的 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.RECEIVED_CASES:
return Object.assign({}, state, {
cases: state.cases.merge(action.cases),
});
default:
return state
}
}
我目前正在使用这个结构,而且效果很好。希望对您有所帮助!
我正在使用 react-native
+ redux
+ immutable
我的应用程序结构如下
/src
-/components
-/actions (where I have action types and actions)
-/reducers
-/api (I'm not sure if I'm doing this right)
好的,所以我有一个动作,我需要像这样用 fetch 进行 API 调用:
import * as types from './casesTypes'
export function getCases() {
return {
type: types.GET_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 the array that I recive from api call)
default:
return state
}
}
所以,问题是我真的不明白,我应该在哪里进行 API 调用,以便在 reducer 中我可以将我的初始状态与我从 API 调用中收到的内容合并?
您应该尝试 redux-thunk or redux-saga,它们是 redux 中间件,用于处理异步操作,例如进行 API 调用。
查看这个使用了 redux-thunk 的项目:sound-redux-native
您的应用程序结构合理。
我还推荐使用 redux-thunk 来处理调度。以下是它在您的情况下的工作方式:
假设您有 'cases' 作为状态树的一部分。 我会按照您建议的方式在您的操作中调用 API 以获取新案例:
import * as types from './casesTypes'
export function getCases() {
return fetch(...)
...
.then((json) => {
dispatch({
type: types.RECEIVED_CASES,
isLoading: false,
cases: json,
}
}
现在在你的 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.RECEIVED_CASES:
return Object.assign({}, state, {
cases: state.cases.merge(action.cases),
});
default:
return state
}
}
我目前正在使用这个结构,而且效果很好。希望对您有所帮助!