Reactjs 是否替换历史 替换:PropTypes.bool, //链接到 至:PropTypes.string }; DelayLink.defaultProps={ 替换:false, 延迟:0, onDelayStart:()=>{}, onDelayEnd:()=>{} }; 导出默认延迟链接;
我将Paul的上述答案修改为一个功能性组件,使其更具最新性:Reactjs 是否替换历史 替换:PropTypes.bool, //链接到 至:PropTypes.string }; DelayLink.defaultProps={ 替换:false, 延迟:0, onDelayStart:()=>{}, onDelayEnd:()=>{} }; 导出默认延迟链接;,reactjs,react-router,Reactjs,React Router,我将Paul的上述答案修改为一个功能性组件,使其更具最新性: import React,{useffect}来自“React”; 从“道具类型”导入道具类型; 从“react router dom”导入{Link,useHistory,useLocation}; //延迟页面导航的功能链接组件 export const DelayLink=props=>{ const{delay,onDelayStart,onDelayEnd,replace,to,…rest}=props; 让timeout=
import React,{useffect}来自“React”;
从“道具类型”导入道具类型;
从“react router dom”导入{Link,useHistory,useLocation};
//延迟页面导航的功能链接组件
export const DelayLink=props=>{
const{delay,onDelayStart,onDelayEnd,replace,to,…rest}=props;
让timeout=null;
让历史=使用历史();
让location=useLocation();
useffect(()=>{
return()=>{
如果(超时){
clearTimeout(超时);
}
};
},[timeout]);
常量handleClick=e=>{
//如果试图导航到当前页面,请停止所有操作
如果(位置?.pathname==to)返回;
启动(e,至);
如果(例如,防止违约){
回来
}
e、 预防默认值();
超时=设置超时(()=>{
如果(更换){
历史。替换(至);
}否则{
历史。推(到);
}
onDelayEnd(e,至);
},延误);
};
回来
};
DelayLink.propTypes={
//注册单击之前等待的毫秒数。
延迟:PropTypes.number,
//在单击链接之后和延迟计时器启动之前调用。
onDelayStart:PropTypes.func,
//在延迟计时器结束后调用。
onDelayEnd:PropTypes.func,
//是否替换历史
替换:PropTypes.bool,
//链接到
至:PropTypes.string
};
DelayLink.defaultProps={
替换:false,
延迟:0,
onDelayStart:()=>{},
onDelayEnd:()=>{}
};
导出默认延迟链接;
最近我遇到了同样的问题,我需要解决。制作此软件包是为了帮助任何需要这样做的人
最近我遇到了同样的问题,我需要解决。制作此软件包是为了帮助任何需要这样做的人
onDelayStart()和onDelayEnd()做什么?我应该传递什么道具?onDelayStart()和onDelayEnd()的作用是什么?我应该传递什么道具?onDelayStart()和onDelayEnd()的作用是什么?我应该为道具传递什么?@panjigemilang这些道具是将在延迟计时器前后运行的函数。因此,onDelayStart在单击时运行,onDelayEnd在计时器用完时运行。为动画等计时很重要。请检查道具类型以了解说明。
onDelayStart()
和onDelayEnd()
做什么?我应该为道具传递什么?@panjigemilang这些道具是将在延迟计时器前后运行的函数。因此,onDelayStart在单击时运行,onDelayEnd在计时器用完时运行。这对你的动画等计时很重要。请查看道具类型以了解描述。对我来说,这是最简单和最好的选择。。谢谢你给了我最简单最好的选择。。非常感谢。
import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
const BasicExample = () => (
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/topics">Topics</Link>
</li>
</ul>
<hr />
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
</div>
</Router>
);
const Home = () => (
<div>
<h2>Home</h2>
</div>
);
const About = () => (
<div>
<h2>About</h2>
</div>
);
const Topics = ({ match }) => (
<div>
<h2>Topics</h2>
<ul>
<li>
<Link to={`${match.url}/rendering`}>Rendering with React</Link>
</li>
<li>
<Link to={`${match.url}/components`}>Components</Link>
</li>
<li>
<Link to={`${match.url}/props-v-state`}>Props v. State</Link>
</li>
</ul>
<Route path={`${match.url}/:topicId`} component={Topic} />
<Route
exact
path={match.url}
render={() => <h3>Please select a topic.</h3>}
/>
</div>
);
const Topic = ({ match }) => (
<div>
<h3>{match.params.topicId}</h3>
</div>
);
export default BasicExample;
import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
/**
* Wraps the React Router Link component and creates a delay after the link is clicked.
*/
export default class DelayLink extends React.Component {
static propTypes = {
/**
* Milliseconds to wait before registering the click.
*/
delay: PropTypes.number,
/**
* Called after the link is clicked and before the delay timer starts.
*/
onDelayStart: PropTypes.func,
/**
* Called after the delay timer ends.
*/
onDelayEnd: PropTypes.func
};
static defaultProps = {
delay: 0,
onDelayStart: () => {},
onDelayEnd: () => {}
};
static contextTypes = Link.contextTypes;
constructor(props) {
super(props);
this.timeout = null;
}
componentWillUnmount() {
if (this.timeout) {
clearTimeout(this.timeout);
}
}
/**
* Called when the link is clicked
*
* @param {Event} e
*/
handleClick = (e) => {
const { replace, to, delay, onDelayStart, onDelayEnd } = this.props;
const { history } = this.context.router;
onDelayStart(e, to);
if (e.defaultPrevented) {
return;
}
e.preventDefault();
this.timeout = setTimeout(() => {
if (replace) {
history.replace(to);
} else {
history.push(to);
}
onDelayEnd(e, to);
}, delay);
};
render() {
const props = Object.assign({}, this.props);
delete props.delay;
delete props.onDelayStart;
delete props.onDelayEnd;
return (
<Link {...props} onClick={this.handleClick} />
);
}
}