如何在 react-countdown 中调用 onStart 函数?
how to call onStart function in react-countdown?
<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
谢谢。
您可以查看文档api of library CountDown component。它提供方法从这里开始。您可以从某些地方开始调用 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>;
}
}}
/>
</>
}
<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
谢谢。
您可以查看文档api of library CountDown component。它提供方法从这里开始。您可以从某些地方开始调用 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>;
}
}}
/>
</>
}