当前页面的数量不会因反应而改变
Number of current page doesnt change in react
我正在使用 react-paginate (https://www.npmjs.com/package/react-paginate) 为我的应用程序分页。一切都很好,但我无法增加页面的当前数量。所以这是我的父组件:
import React, { useEffect, useState } from "react";
import Product from "../components/sub-components/Product";
import SimpleBox from "../components/sub-components/SimpleBox";
import BoxWithSearch from "../components/sub-components/BoxWithSearch";
import LoadingBox from "../components/sub-components/LoadingBox";
import MessageBox from "../components/sub-components/MessageBox";
import Cart from "../components/sub-components/Cart";
import { useDispatch, useSelector } from "react-redux";
import { listProducts } from "../actions/productActions";
import ReactPaginate from "react-paginate";
export default function HomeScreen() {
const dispatch = useDispatch();
const productList = useSelector((state) => state.productList);
const { loading, error, products } = productList;
const [currentPage, setCurrentPage] = useState(1);
const [pageCount, setpageCount] = useState(0);
useEffect(() => {
dispatch(listProducts(currentPage));
console.log(currentPage);
}, [dispatch]);
const handlePageClick = (data) => {
setCurrentPage(data.selected + 1);
// scroll to the top
//window.scrollTo(0, 0)
};
return (
<div className="container">
<div className="row">
<div className="col-lg-6 col-md-12 col-sm-12 col-xs-12">
<h2 className="title">Products</h2>
<div className="product-type-filter">
<button>Mug</button>
<button className="clicked">Shirt</button>
</div>
<div className="products">
<div className="row">
<div>
{loading ? (
<LoadingBox></LoadingBox>
) : error ? (
<MessageBox variant="danger">{error}</MessageBox>
) : (
<div className="row center">
{products.map((product) => (
<Product key={product.added} product={product}></Product>
))}
</div>
)}
</div>
</div>
</div>
<ReactPaginate
previousLabel={"Prev"}
nextLabel={"Next"}
pageCount={40}
marginPagesDisplayed={4}
pageRangeDisplayed={1}
onPageChange={handlePageClick}
containerClassName={"pagination justify-content-center"}
pageClassName={"page-item"}
pageLinkClassName={"page-link"}
previousClassName={"page-item"}
previousLinkClassName={"page-link"}
nextClassName={"page-item"}
nextLinkClassName={"page-link"}
breakClassName={"page-item"}
breakLinkClassName={"page-link"}
activeClassName={"active"}
/>
</div>
</div>
</div>
);
}
以及我获取数据的操作:
import Axios from 'axios';
import {
PRODUCT_LIST_FAIL,
PRODUCT_LIST_REQUEST,
PRODUCT_LIST_SUCCESS,
} from '../constants/productConstants';
export const listProducts = () => async (dispatch, currentPage) => {
dispatch({
type: PRODUCT_LIST_REQUEST,
});
try {
const { data } = await Axios.get(`http://localhost:3000/items?_page=${currentPage}&_limit=16`);
dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
} catch (error) {
dispatch({ type: PRODUCT_LIST_FAIL, payload: error.message });
}
};
但问题是因为currentPage没有改变,所以我无法转到其他页面。你有解决办法吗?
谢谢...
如果您正在更新当前页面并希望获取新数据,那么您可能需要将 currentPage
添加到 useEffect
依赖项数组,以便 next 当前产品页面是 fetched/listed.
useEffect(() => {
dispatch(listProducts(currentPage));
console.log(currentPage);
}, [currentPage, dispatch]);
更新
When I write console.log(currentPage)
in action, I am getting this:
ƒ getState() { var state = unliftState(liftedStore.getState()); if (state !== undefined) { lastDefinedState = state; } return lastDefinedState; }
How can I pass the currentpage
number into
action?
在 thunks 中,第二个参数是 getState
要调用的函数并获取当前的 redux 状态。您的 listProducts
动作创建者正在命名 getState
回调 currentPage
。此外,任何传递给 listProducts
的参数都将被忽略(注意外部函数中的空参数列表)。
export const listProducts = () => async (dispatch, currentPage) => {
dispatch({
type: PRODUCT_LIST_REQUEST,
});
try {
const { data } = await Axios.get(`http://localhost:3000/items?_page=${currentPage}&_limit=16`);
dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
} catch (error) {
dispatch({ type: PRODUCT_LIST_FAIL, payload: error.message });
}
};
listProducts
需要在外部函数中使用传递的 currentPage
参数,并将其包含在函数范围内。
export const listProducts = (currentPage) => async (dispatch) => {
dispatch({
type: PRODUCT_LIST_REQUEST,
});
try {
const { data } = await Axios.get(`http://localhost:3000/items?_page=${currentPage}&_limit=16`);
dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
} catch (error) {
dispatch({ type: PRODUCT_LIST_FAIL, payload: error.message });
}
};
我正在使用 react-paginate (https://www.npmjs.com/package/react-paginate) 为我的应用程序分页。一切都很好,但我无法增加页面的当前数量。所以这是我的父组件:
import React, { useEffect, useState } from "react";
import Product from "../components/sub-components/Product";
import SimpleBox from "../components/sub-components/SimpleBox";
import BoxWithSearch from "../components/sub-components/BoxWithSearch";
import LoadingBox from "../components/sub-components/LoadingBox";
import MessageBox from "../components/sub-components/MessageBox";
import Cart from "../components/sub-components/Cart";
import { useDispatch, useSelector } from "react-redux";
import { listProducts } from "../actions/productActions";
import ReactPaginate from "react-paginate";
export default function HomeScreen() {
const dispatch = useDispatch();
const productList = useSelector((state) => state.productList);
const { loading, error, products } = productList;
const [currentPage, setCurrentPage] = useState(1);
const [pageCount, setpageCount] = useState(0);
useEffect(() => {
dispatch(listProducts(currentPage));
console.log(currentPage);
}, [dispatch]);
const handlePageClick = (data) => {
setCurrentPage(data.selected + 1);
// scroll to the top
//window.scrollTo(0, 0)
};
return (
<div className="container">
<div className="row">
<div className="col-lg-6 col-md-12 col-sm-12 col-xs-12">
<h2 className="title">Products</h2>
<div className="product-type-filter">
<button>Mug</button>
<button className="clicked">Shirt</button>
</div>
<div className="products">
<div className="row">
<div>
{loading ? (
<LoadingBox></LoadingBox>
) : error ? (
<MessageBox variant="danger">{error}</MessageBox>
) : (
<div className="row center">
{products.map((product) => (
<Product key={product.added} product={product}></Product>
))}
</div>
)}
</div>
</div>
</div>
<ReactPaginate
previousLabel={"Prev"}
nextLabel={"Next"}
pageCount={40}
marginPagesDisplayed={4}
pageRangeDisplayed={1}
onPageChange={handlePageClick}
containerClassName={"pagination justify-content-center"}
pageClassName={"page-item"}
pageLinkClassName={"page-link"}
previousClassName={"page-item"}
previousLinkClassName={"page-link"}
nextClassName={"page-item"}
nextLinkClassName={"page-link"}
breakClassName={"page-item"}
breakLinkClassName={"page-link"}
activeClassName={"active"}
/>
</div>
</div>
</div>
);
}
以及我获取数据的操作:
import Axios from 'axios';
import {
PRODUCT_LIST_FAIL,
PRODUCT_LIST_REQUEST,
PRODUCT_LIST_SUCCESS,
} from '../constants/productConstants';
export const listProducts = () => async (dispatch, currentPage) => {
dispatch({
type: PRODUCT_LIST_REQUEST,
});
try {
const { data } = await Axios.get(`http://localhost:3000/items?_page=${currentPage}&_limit=16`);
dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
} catch (error) {
dispatch({ type: PRODUCT_LIST_FAIL, payload: error.message });
}
};
但问题是因为currentPage没有改变,所以我无法转到其他页面。你有解决办法吗? 谢谢...
如果您正在更新当前页面并希望获取新数据,那么您可能需要将 currentPage
添加到 useEffect
依赖项数组,以便 next 当前产品页面是 fetched/listed.
useEffect(() => {
dispatch(listProducts(currentPage));
console.log(currentPage);
}, [currentPage, dispatch]);
更新
When I write
console.log(currentPage)
in action, I am getting this:ƒ getState() { var state = unliftState(liftedStore.getState()); if (state !== undefined) { lastDefinedState = state; } return lastDefinedState; }
How can I pass thecurrentpage
number into action?
在 thunks 中,第二个参数是 getState
要调用的函数并获取当前的 redux 状态。您的 listProducts
动作创建者正在命名 getState
回调 currentPage
。此外,任何传递给 listProducts
的参数都将被忽略(注意外部函数中的空参数列表)。
export const listProducts = () => async (dispatch, currentPage) => {
dispatch({
type: PRODUCT_LIST_REQUEST,
});
try {
const { data } = await Axios.get(`http://localhost:3000/items?_page=${currentPage}&_limit=16`);
dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
} catch (error) {
dispatch({ type: PRODUCT_LIST_FAIL, payload: error.message });
}
};
listProducts
需要在外部函数中使用传递的 currentPage
参数,并将其包含在函数范围内。
export const listProducts = (currentPage) => async (dispatch) => {
dispatch({
type: PRODUCT_LIST_REQUEST,
});
try {
const { data } = await Axios.get(`http://localhost:3000/items?_page=${currentPage}&_limit=16`);
dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
} catch (error) {
dispatch({ type: PRODUCT_LIST_FAIL, payload: error.message });
}
};