[英]ACTION didn't attach to reducer despite state of reducer in react-redux
I put logic of action for getting API DATA by AXIOS, then as dispatch method I tried to put received datas into state. 我提出了通过AXIOS获取API数据的动作逻辑,然后作为调度方法,我试图将接收到的数据置于状态。 But at this point actions didn't go to reducer. 但是此时动作还没有归结为减速器。
(6) [{…}, {…}, {…}, {…}, {…}, {…}]
index.js:23 Uncaught (in promise) TypeError: dispatch is not a function
at index.js:23
Just I got that error above. 只是我上面有这个错误。 This means this action did get a API data then when it try to dispatch, failed. 这意味着此操作确实获取了API数据,然后在尝试分派时失败了。 I could find the connection point of this. 我可以找到这个的连接点。
I attach some JavaScript: 我附上一些JavaScript:
reducer.js: 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 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: 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);
You need to have your action creators return functions that perform the asynchronous requests, right now your action creators only return objects, we can return functions using redux-thunk middleware 您需要让操作创建者返回执行异步请求的函数,现在您的操作创建者仅返回对象,我们可以使用redux-thunk中间件来返回函数
Then you would write your action creators that make api calls like this 然后,您将编写这样的动作创建者来进行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
})
);
};
Thank you guys who made a answer for my question, actually I solved this problem a quite easy way. 谢谢回答我的问题的人,实际上我是用一种很简单的方法解决了这个问题。
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;
I planned to use SAGA after making this, but once I change middleware as thunk, it worked out well. 我计划在完成此操作后使用SAGA,但是一旦我将中间件更改为thunk,它就会很好地工作。 Maybe I need to figure out how thunk works out. 也许我需要弄清楚thunk的工作原理。
Even if I prefer to use SAGA for this time I want to say thank you for Thunk. 即使我这次更喜欢使用SAGA,我还是要感谢Thunk。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.