gpt4 book ai didi

reactjs - 在 Axios 中使用 Redux-Observable 的问题

转载 作者:行者123 更新时间:2023-12-05 08:40:44 25 4
gpt4 key购买 nike

我正在尝试学习 redux-observables,但我似乎无法让我的应用程序返回数据。我不断收到以下错误消息,但我不确定哪里出了问题或错误的实际含义。

错误:类型错误:axios__WEBPACK_IMPORTED_MODULE_3___default.a.get(...).map 不是函数

Action :

import { FETCH_DATA, FETCH_DATA_FAIL } from './constants';

export const fetchData = exampleData => ({
type: FETCH_DATA,
payload: { exampleData }
});

export const fetchDataFail = () => ({
type: FETCH_DATA_FAIL
});

史诗:

 import 'rxjs';
import { FETCH_DATA, FETCH_DATA_FAIL } from './constants';
import { fetchData } from './actions';
import axios from 'axios';
import { Observable } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import { ofType } from 'redux-observable';

export const exampleEpic = action$ =>
action$.pipe(ofType(FETCH_DATA),
mergeMap(action =>
axios.get('https://jsonplaceholder.typicode.com/todos/1')
.map(response => fetchData(response))
.catch(error => Observable.ofType(FETCH_DATA_FAIL)))
);

reducer :

     import { FETCH_DATA, FETCH_DATA_FAIL } from './constants';
import { combineReducers } from 'redux';

const initialState = {};

export const exampleData = (state = initialState, action) => {
switch (action.type) {
case FETCH_DATA:
return action.payload
case FETCH_DATA_FAIL:
return {};
default:
return state;
}
};

export default combineReducers({
exampleData
});

示例组件:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchData } from './actions';

class App extends Component {

onClick = ()=>{
this.props.fetchData();
}

render() {
return (
<div>
<button onClick={this.onClick}><h1>Hello World</h1></button>
{JSON.stringify(this.props.data) }
</div>
);
}
}

const mapStateToProps = (state) => {
return {
data: state
}
};

function mapDispatchToProps(dispatch) {
return {
fetchData: () => dispatch(fetchData())
}
};

export default connect(
mapStateToProps,
mapDispatchToProps
)(App);

最佳答案

使用 .then 可以解决问题,正确的做法是使用 rxjs 的 from 函数,如下所示

 import 'rxjs';
import { FETCH_DATA, FETCH_DATA_FAIL } from './constants';
import { fetchData } from './actions';
import axios from 'axios';
import { Observable, from } from 'rxjs';
import { mergeMap, map } from 'rxjs/operators';
import { ofType } from 'redux-observable';

export const exampleEpic = action$ =>
action$.pipe(ofType(FETCH_DATA),
mergeMap(action =>
from(axios.get('https://jsonplaceholder.typicode.com/todos/1'))
.map(response => fetchData(response))
.catch(error => Observable.ofType(FETCH_DATA_FAIL)))
);

此外,确保从 rxjs 导入 .map 运算符

更新:使用 .pipe 运算符的语法

export const exampleEpic = action$ =>
action$.pipe(
ofType(FETCH_DATA),
mergeMap(action => from(axios.get('https://jsonplaceholder.typicode.com/todos/1/'))
.pipe(
map(response => fetchData(response)),
catchError(() => of(fetchDataFailure()))
)
)
)

关于reactjs - 在 Axios 中使用 Redux-Observable 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54119687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com