reactjs - 如何在 react-countdown 中调用 onStart 函数?
问题描述
<Countdown
date={Date.now() + (n.duration)}
ref={this.refCallback}
autoStart={false}
renderer={({ hours, minutes, seconds, completed }) => {
if (completed) {
// Render a completed state
return <div>Times up</div>;
} else {
// Render a countdown
return <h1 className="m-0 font-weight-bold">{hours}:{minutes}:{seconds}</h1>;
}
}}
/>
这是文档 https://www.npmjs.com/package/react-countdown
谢谢。
解决方案
您可以查看库 CountDown 组件的文档 api。它提供了方法 start here。您可以调用this.refCallback.start()
一些从哪里开始。例子:
const Component =(props) => {
const ref= useRef();
const handleStart = (e) => {
ref.current?.start();
}
const handlePause = (e) => {
ref.current?.pause();
}
return <>
<button onClick={handleStart}> Start </button>
<button onClick={handlePause}> Pause </button>
<Countdown
date={Date.now() + (20000)}
ref={ref}
autoStart={false}
renderer={({ hours, minutes, seconds, completed }) => {
if (completed) {
// Render a completed state
return <div>Times up</div>;
} else {
// Render a countdown
return <h1 className="m-0 font-weight-bold">{hours}:{minutes}:{seconds}</h1>;
}
}}
/>
</>
}
推荐阅读
- css - 想要在 .net 项目中修复我的 .cshtml 中的 CSS 和引导程序问题
- java - 尝试打开 MainActivity 时出现奇怪的错误代码
- asp.net - 在项目中发布或更新单个 DLL - 安全吗?
- c# - 在更新 c# 实体框架核心时删除旧文件
- c# - Asp.net Mvc c#动态会话
- c# - 如何拥有只读成员的 C# 接口
- hyperledger-fabric - 从对等点加入频道时如何启用发现角色
- alfresco - 使用 CMIS API 上传重复文件
- json - 将空字符串传递给 Class JsonOutput 方法 toJSON
- javascript - 如何将 unit8array 转换为字符以将其与 bcrypt 一起使用?