![](/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.