Use fetch instead of ajax with redux-observable

前端 未结 2 1749
庸人自扰
庸人自扰 2020-12-10 12:56

In redux-observable is it possible to use isomporphic-fetch instead of Rx.DOM.ajax?

2条回答
  •  伪装坚强ぢ
    2020-12-10 13:44

    Did small adjustment to @jayphelps in order to get that code work for me. Hope this helps to save someone's time.

    import { FETCH_USER } from './actions'
    import { ofType } from 'redux-observable'
    import { map, mergeMap } from 'rxjs/operators'
    import { from } from 'rxjs'
    
    const fetchUserEpic = action$ => {
        return action$.pipe(
            ofType(FETCH_USER),
            mergeMap((action = { user: 'redux-observable' }) => {
                const getRequest = (user) => {
                    const request = fetch(`https://api.github.com/users/${user}`)
                        .then(response => response.json())
                    return from(request)
                }
    
                return getRequest(action.user).pipe(
                   map(payload => ({ type: FETCH_USER_FULFILLED, payload }))
                )
            })
        )
    }
    

提交回复
热议问题