![](/img/trans.png)
[英]How to render another react component on a single button click? (Functional Component)
[英]How to destroy and reinitialize component in react on Button click in another component
我從這樣的 React 教程中創建了一個分頁,
const [posts, setPosts] = useState([]);
const [showPerPage] = useState(6);
const [pagination, setPagination] = useState({
start: 0,
end: showPerPage
});
const onPaginationChange = (start, end) => {
setPagination({start: start, end: end});
}
以上幾行在我調用分頁組件的主組件中,如下所示:
<Pagination showPerPage={showPerPage} onPaginationChange={onPaginationChange} totalProvider={posts.length}/>
現在我已經創建了這樣的分頁組件,
import React, {useState, useEffect} from 'react' const Pagination = ({showPerPage, onPaginationChange, totalProvider}) => { const [counter, setCounter] = useState(1); useEffect(()=>{ const value = showPerPage * counter; onPaginationChange(value - showPerPage, value); },[counter]); const onButtonClick = (type) => { if(type==='prev'){ if(counter === 1){ setCounter(1); }else { setCounter(counter - 1); } }else if( type=== 'next'){ if(Math.ceil(totalProvider/showPerPage)=== counter){ setCounter(counter); }else { setCounter(counter + 1); } } } return ( <div className="paginationWrapper"> <span id="prevBtn" onClick={()=>onButtonClick('prev')} className="disable">Prev.</span> <div className="numberOfItems"> <span id="startCounter">{counter}</span>of<span>{Math.ceil(totalProvider/showPerPage)}</span> </div> <span id="nextBtn" onClick={()=>onButtonClick('next')}>Next</span> </div> ) } export default Pagination
這是我的設置,現在我想在單擊父組件中的按鈕時重置分頁,因為在該按鈕上單擊我需要獲取更多或更少的數據,在這種情況下,我的分頁始終保持在我之前離開了它,我可以通過兩種方式做到這一點,但無法獲取代碼方式:
請幫忙,注意:我沒有使用 redux。
我認為使用 ref 是最簡單的方法。
import React, {userRef} from "react"
function ParentComponent() {
const resetRef = useRef();
const handleReset = () => {
if (resetRef && resetReft.current) resetRef.current.resetCounter();
}
return <>
<button onClick={handleReset}>Reset</button>
<Pagination ref={resetRef} showPerPage={showPerPage} onPaginationChange={onPaginationChange} totalProvider={posts.length}/>
</>
}
將 forwardRef 和 resetCounter() 添加到您的分頁組件。
const Pagination = React.forwardRef(({showPerPage, onPaginationChange, totalProvider}, ref) => {
const [counter, setCounter] = useState(1);
useEffect(()=>{
const value = showPerPage * counter;
onPaginationChange(value - showPerPage, value);
},[counter]);
const onButtonClick = (type) => {
if(type==='prev'){
if(counter === 1){
setCounter(1);
}else {
setCounter(counter - 1);
}
}else if( type=== 'next'){
if(Math.ceil(totalProvider/showPerPage)=== counter){
setCounter(counter);
}else {
setCounter(counter + 1);
}
}
}
resetCounter() {
setCounter(1);
}
return (
<div className="paginationWrapper" ref={ref}>
<span id="prevBtn" onClick={()=>onButtonClick('prev')} className="disable">Prev.</span>
<div className="numberOfItems">
<span id="startCounter">{counter}</span>of<span>{Math.ceil(totalProvider/showPerPage)}</span>
</div>
<span id="nextBtn" onClick={()=>onButtonClick('next')}>Next</span>
</div>
)
})
export default Pagination
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.