비즈니스 로직을 분리하는 것이 좋습니다. Redux를 사용할 계획이라면, 모델과 서비스를 별도의 패키지에 생성하고 필요할 때마다 가져올 수 있습니다.
모델 - 상태를 정의하고 축소 자 및 효과를 포함합니다. 서비스 - RestFul API를 호출하는 실제 함수.
모델 < -> 서비스 접근 방식은 UI 요소가 전혀 없기 때문에 매우 유용합니다.
모델 및 아래 서비스의 예를 참조하십시오
가 모델 :이
import {
addPayment
} from '../services/paymentService'
import {
updateAge
} from '../services/profileService'
export default {
namespace: 'myProfile',
state: {
age
},
reducers: {
set: (state, {payload: details}) => {
return ({...state, ...details})
},
},
effects: {
*getAge (action, { call, put, simplePut }) {
const {payload: age} = action
try {
const res = yield call(upadteAge, age)
const { status } = res
if (status === 200) {
const json = yield res.json()
yield simplePut('set', json)
}
} catch (error) {
console.log(error)
}
},
}
서비스 : 나는 최근에 응용 프로그램을 완료
export function updateAge ({age}) {
return fetch('apiurl?age=' + age, {
method: 'POST',
headers: {
}
})
}
이있는 내가 사용한 위의 접근 방식과 매력처럼 작동합니다.