我提出了通过AXIOS获取API数据的动作逻辑,然后作为调度方法,我试图将接收到的数据置于状态。但是目前,动作还没有归结到减速器上。
(6) [{…}, {…}, {…}, {…}, {…}, {…}]
index.js:23 Uncaught (in promise) TypeError: dispatch is not a function
at index.js:23
我刚刚在上面遇到了这个错误。这意味着此操作确实获取了API数据,然后在尝试分派时失败了。我可以找到它的连接点。
我附上了一些JavaScript:
reducer.js:
import * as types from '../Actions/Types';
const initialState = {
contents: [{
poster: 'https://i.imgur.com/633c18I.jpg',
title: 'state title',
overview: 'state overview',
id: 123,
}],
content: {},
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case types.LOADING_DATA: {
console.log(`something happend ${action.payload}`);
return state.set('contents', action.payload);
}
case types.BTN_ON_CHANGE: {
return state;
}
case types.BTN_ON_CLICK: {
return state;
}
case types.BTN_ON_SUBMIT: {
return state;
}
default:
return state;
}
};
export default reducer;
actions.js
import axios from 'axios';
import * as types from './Types';
const holder = [];
const API_GET = () => (
axios.get('https://api.themoviedb.org/3/search/movie?api_key=<<APIKEY>>&query=avengers+marvel')
.then(res => res.data)
.then(data => console.log(data.results))
.then(results => holder.add(results))
);
// export const loadingData = value => ({
// type: types.LOADING_DATA,
// value,
// });
export const loadingData = () => (dispatch) => {
axios.get('https://api.themoviedb.org/3/search/movie?api_key=54087469444eb8377d671f67b1b8595d&query=avengers+marvel')
.then(res => res.data)
.then(data => console.log(data.results))
.then(results => dispatch({
type: types.LOADING_DATA,
payload: results,
}));
};
export const sample = () => (
console.log('none')
);
LoadingDataButton.js:
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Button } from 'antd';
import { loadingData } from '../Actions';
const LoadingDataButton = props => (
<div>
<Button
type="danger"
onClick={
props.loadingData()
}
>
Loading
</Button>
</div>
);
LoadingDataButton.propTypes = {
loadingData: PropTypes.func.isRequired,
};
const mapStateToProps = state => ({
contents: state.contentR.contents,
});
const mapDispatchToState = {
loadingData,
};
export default connect(mapStateToProps, mapDispatchToState)(LoadingDataButton);
答案 0 :(得分:0)
您需要让动作创建者返回执行异步请求的函数,现在您的动作创建者仅返回对象,我们可以使用redux-thunk middleware
返回函数然后,您将编写这样的动作创建者来进行api调用
export const fetchThreadData = id => dispatch => {
dispatch({
type: FETCH_THREADS_REQUESTING,
isLoading: true
});
const request = axios({
method: "GET",
url: "SOME API ADDRESS"
});
return request.then(
response =>
dispatch({
type: FETCH_THREADS_SUCCESSFUL,
payload: response.data,
isLoading: false
}),
error =>
dispatch({
type: FETCH_THREADS_FAILED,
payload: error || "Failed to fetch thread",
isLoading: false
})
);
};
答案 1 :(得分:0)
谢谢你们回答我的问题,实际上,我以一种非常简单的方式解决了这个问题。
import { createStore, applyMiddleware, compose } from 'redux';
// import saga from 'redux-saga';
import thunk from 'redux-thunk';
import rootReducer from './rootReducer';
const initialState = {};
const middleWare = [thunk];
const store = createStore(
rootReducer,
initialState,
compose(
applyMiddleware(...middleWare),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
),
);
export default store;
我打算在完成此操作后使用SAGA,但是一旦我将中间件更改为thunk,它就会很好地工作。也许我需要弄清楚thunk的工作原理。
即使这次我更喜欢使用SAGA,我也想对Thunk表示感谢。