【发布时间】:2020-12-01 08:16:59
【问题描述】:
import React, {useState, useEffect} from 'react'; import {connect} from 'react-redux'; import { fetchRecipes } from '../../store/actions'; import './BeerRecipes.css'; const BeerRecipes = ({recipesData, fetchRecipes}) => { const [page, setPage] = useState(1); const [recipes, setRecipes] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { fetchRecipes(); }, []) return ( <div className='beer_recipes_block'> <div className='title_wrapper'> <h2 className='title'>Beer recipes</h2> </div> <div className='beer_recipes'> <ul className='beer_recipes_items'> { recipesData && recipesData.recipes && recipesData.recipes.map(recipe => <li className='beer_recipes_item' id={recipe.id}>{recipe.name}</li> ) } </ul> </div> </div> ); }; const mapStateToProps = state => { return { recipesData: state.recipes } } const mapDispatchToProps = dispatch => { return { fetchRecipes: () => dispatch(fetchRecipes()) } } export default connect(mapStateToProps, mapDispatchToProps)(BeerRecipes);
这是我想要创建无限滚动的组件,下面是我使用 axios 的 redux-action:
import axios from "axios"; import * as actionTypes from "./actionTypes"; export const fetchRecipesRequest = () => { return { type: actionTypes.FETCH_RECIPES_REQUEST } } export const fetchRecipesSuccess = recipes => { return { type: actionTypes.FETCH_RECIPES_SUCCESS, payload: recipes } } export const fetchRecipesFailure = error => { return { type: actionTypes.FETCH_RECIPES_FAILURE, payload: error } } export const fetchRecipes = (page) => { return (dispatch) => { dispatch(fetchRecipesRequest) axios .get('https://api.punkapi.com/v2/beers?page=1') .then(response => { const recipes = response.data; dispatch(fetchRecipesSuccess(recipes)); }) .catch(error => { const errorMsg = error.message; dispatch(fetchRecipesFailure(errorMsg)); }) } }
我想创建一个卷轴。首先,我需要显示前 10 个元素,然后在每次加载时添加 5 个元素。我总共有 25 个元素,当列表完成后,它应该再次从前五个开始。
【问题讨论】: