簡體   English   中英

如何銷毀和重新初始化組件以響應另一個組件中的按鈕單擊

[英]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

這是我的設置,現在我想在單擊父組件中的按鈕時重置分頁,因為在該按鈕上單擊我需要獲取更多或更少的數據,在這種情況下,我的分頁始終保持在我之前離開了它,我可以通過兩種方式做到這一點,但無法獲取代碼方式:

  1. 如果我可以將計數器(分頁組件中的 const )重置為 1,單擊該按鈕,則可以完成,或者
  2. 如果我可以只銷毀組件,它的每個 State 就可以了。

請幫忙,注意:我沒有使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM