reactjs - React & Redux: Infinite Scroll 导致 TypeError: Dispatch is not a Function 错误
问题描述
当用户继续滚动(无限滚动)时,我使用react-infinite-scroll-component
包作为加载数据的一种方式。但是,当我使用 Redux 和所有内容进行设置时,如果我滚动到底部,我的操作方法会出现以下错误:
TypeError: dispatch is not a function
这些是我用于复制或更多上下文的文件:
TopRatedComponent.js:
import React, { useState, useEffect } from "react";
import { useSelector } from "react-redux";
import { connect } from "react-redux";
import { fetchTopRatedMovies } from '../actions/topRatedAction.js';
import { Container, Row, Col, Button } from 'react-bootstrap';
import { useDispatch } from "react-redux";
import InfiniteScroll from "react-infinite-scroll-component";
const TopRatedComponent = (props) => {
const dispatch = useDispatch();
let [page, setPage] = useState(3);
useEffect(() => {
dispatch(fetchTopRatedMovies(page));
setPage = page++;
}, [])
const {
topRatedMovies,
loading,
error
} = useSelector(state => state.topRatedMovies);
return (
<div >
{loading && <div>LOADING...</div>}
{error && <div>{error}</div>}
<Container className="p-4" >
<InfiniteScroll
dataLength={20}
next={fetchTopRatedMovies(page)}
hasMore={true}
loader={<h4>Loading...</h4>}
>
{topRatedMovies.results && topRatedMovies.results.map(topRated => (
<div key={topRated.id}>
div - #{topRated.title}
</div>
))}
</InfiniteScroll>
</Container>
</div>
)
};
const mapStateToProps = state => {
const { topRatedMovies, loading, error } = state.topRatedMovies;
return {
topRatedMovies,
loading,
error
};
};
export default connect(
mapStateToProps,
{
fetchTopRatedMovies
}
)(TopRatedComponent);
商店.js:
import { createStore, compose, applyMiddleware, combineReducers } from "redux";
import thunkMiddleware from 'redux-thunk'
import topRatedReducer from '../reducers/topRatedReducer'
const rootReducer = combineReducers({
topRatedMovies: topRatedReducer
});
const store = createStore(rootReducer, compose(applyMiddleware(thunkMiddleware),
));
export default store;
topRatedAction.js:
import {
FETCH_TOPRATED_START,
FETCH_TOPRATED_FAILURE,
FETCH_TOPRATED_SUCCESS
} from './actionTypes'
import axios from "axios";
const API_KEY = 'MyAPIKEY';
export const fetchTopRatedMovies = (page) => {
// page = 1;
const API_URL = `https://api.themoviedb.org/3/movie/top_rated?api_key=${API_KEY}&language=en-US&page=` + page;
return dispatch => {
dispatch(fetchTopRatedStarted());
axios.get(API_URL)
.then(response => {
dispatch(fetchTopRatedSucceeded(response.data));
})
.catch(error =>{
dispatch(fetchTopRatedFailed(error.message));
});
};
};
const fetchTopRatedStarted = () => {
return{
type: FETCH_TOPRATED_START,
payload: {
isLoading: true
}
};
};
const fetchTopRatedSucceeded = topRatedMovies => {
return{
type: FETCH_TOPRATED_SUCCESS,
payload: {
topRatedMovies
}
};
};
const fetchTopRatedFailed = error => {
return{
type: FETCH_TOPRATED_FAILURE,
payload: {
error
}
};
};
topRatedReducer.js
import {
FETCH_TOPRATED_START,
FETCH_TOPRATED_FAILURE,
FETCH_TOPRATED_SUCCESS
} from '../actions/actionTypes'
const initialState = {
topRatedMovies: [],
loading: false,
error: null
}
export default function (state = initialState, action){
switch (action.type) {
case FETCH_TOPRATED_START:
return{
...state,
loading: true
};
case FETCH_TOPRATED_FAILURE:
return{
...state,
loading: false,
error: action.payload.error
}
case FETCH_TOPRATED_SUCCESS:
return{
...state,
loading:false,
topRatedMovies: action.payload.topRatedMovies
};
default:
return state
}
}
解决方案
const infinite = () => {
dispatch(fetchTopRatedMovies(page));
setPage=page++;
};
然后从下一个调用无限
下一个={无限}
推荐阅读
- spring - Spring data JPA + Native query - 根据配置文件替换查询字符串
- asp.net-mvc - 在 AZURE 中通过安全透明方法“System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule()”尝试
- pandas - 有没有办法在 python 中优化 cumprod?
- ios - “id”类型如何在不强制转换的情况下理解方法的接收者?
- ghostscript - 使用 ghostscript 将 pdf 转换为具有颜色配置文件的其他格式
- alexa - Alexa notifications: The authentication token is not valid
- c# - System.AccessViolationException 发生在 System.Net.Http C#
- java - 从corda Flow返回HashMap到SpringBoot抛出:com.esotericsoftware.kryo.KryoException
- automation - 使用 Automation Anywhere 自动化 SAP 的问题
- reactjs - REACT NATIVE - 关闭导入模式时更改屏幕状态