react-compound-timer icon indicating copy to clipboard operation
react-compound-timer copied to clipboard

Is there any way to set initialTime by a function?

Open KongfuMan opened this issue 5 years ago • 15 comments

I need to set initialTime to be some value from previous calculation. Can I set that value at constructor?

KongfuMan avatar May 08 '19 19:05 KongfuMan

Do you need the value of intialTime to be dynamic when your component mounts?

an0nEmouse avatar May 09 '19 03:05 an0nEmouse

Do you need the value of intialTime to be dynamic when your component mounts? Exactly

KongfuMan avatar May 09 '19 20:05 KongfuMan

Now I am using " Change props dynamically and use HOC" of this https://volkov97.github.io/react-compound-timer/ So I am wondering how to set intialTime when the component mounts. Thank you!

KongfuMan avatar May 09 '19 20:05 KongfuMan

I used setTime in the constructor, but the timer does not update. When the timer starts, it continues from the time I set.

KongfuMan avatar May 09 '19 21:05 KongfuMan

maybe you should try to call setTime in componentDidMount, not in constructor can you check it?

volkov97 avatar May 10 '19 15:05 volkov97

I just tried, but it doesn't work. This is my code: ` const withTimer = timerProps => WrappedComponent => wrappedComponentProps => ( <Timer {...timerProps}> {timerRenderProps => <WrappedComponent {...wrappedComponentProps} timer={timerRenderProps} />} </Timer> ); class StepTimer extends Component { constructor(props){ super(props); const { setTime } = this.props.timer; setTime(this.props.currentTime); }

componentDidMount(){
    this.props.onRef&&this.props.onRef(this.props.timer.reset);
}

componentDidUpdate(){
    const { start, pause } = this.props.timer;
    if (this.props.dataStreamRequested === streamState.STARTED){
        start();
    }else{
        pause();
    }
}

componentWillUnmount(){
    const { getTime } = this.props.timer;
    this.props.updateTime(getTime());
}

render() {
    return (
        <Text style={{fontSize:20}}>
            <Timer.Minutes initialTime={10000} />:<Timer.Seconds />
        </Text>
    );
}

}

const SessionTimer = withTimer({ initialTime: 0, //how to update this value? startImmediately: false, })(StepTimer);`

I guess it may be caused by the execution order of withTimer and componentDidMount?

KongfuMan avatar May 10 '19 17:05 KongfuMan

i mean, that you should call this.props.timer. setTime(10000) in componentDidMount why are you calling reset? and Timer.Minutes component has no such property initialTime

volkov97 avatar May 11 '19 08:05 volkov97

I had the same problem when I'm trying to set another initla time by property from parent component.

Can you please create a simple example, how I can change my initial time depending on prop from parent component please?

avernikoz avatar Jul 11 '19 17:07 avernikoz

same problem

sondosmustafa avatar Apr 06 '20 15:04 sondosmustafa

Same problem here too!

kyds3k avatar Aug 30 '20 11:08 kyds3k

same problem

codebefore avatar Sep 28 '20 12:09 codebefore

To change the initial time based on a property, use useTimer

import { useTimer } from 'react-compound-timer'
export default function Timer(props) {
    const {
        value,
        controls: { setTime, reset }
    } = useTimer({ initialTime: 0 })

    useEffect(() => {
        setTime(props.initialTime)
    }, [props.initialTime]);
    return (
        <div>
            <h2>{value.m}</h2>
            <h2>{value.s}</h2>
        </div>
    )
}

danielm2402 avatar Oct 27 '20 18:10 danielm2402

how can I update the initialTime of the Timer in a class based component? I am setting the initialTime based on state, but the timer's initialTime doesn't update even when the value in state updates.

nilayvr14 avatar Dec 14 '20 07:12 nilayvr14

To change the initial time based on a property, use useTimer

import { useTimer } from 'react-compound-timer'
export default function Timer(props) {
    const {
        value,
        controls: { setTime, reset }
    } = useTimer({ initialTime: 0 })

    useEffect(() => {
        setTime(props.initialTime)
    }, [props.initialTime]);
    return (
        <div>
            <h2>{value.m}</h2>
            <h2>{value.s}</h2>
        </div>
    )
}

Can you pls use this in an example

Perelyn-sama avatar Dec 16 '20 20:12 Perelyn-sama

hey, check out this comment worked great for me!

for any one who is wondering, here is my code:

` import { FC, useEffect, useRef, useState } from 'react'; import './TimerComponent.css'; import React from 'react'; import Timer from 'react-compound-timer' import ITimer from '../../Interfaces/ITimer'

const TimerComponent: FC<ITimer> = (props: ITimer) => { const stopwatchControl = useRef(null);

useEffect(() => {
    changeTimerState(props.command);

}, [props.command, props.startTime]);

const changeTimerState = (timerState: string) => {
    switch (timerState) {
        case "start":
            stopwatchControl.current.start()
            break;
        case "stop":
            stopwatchControl.current.stop()
            break;
        case "reset":
            stopwatchControl.current.reset();
            break;
        default: console.log("error with stopwatch")
            break;
    }
}

return (
    <div key={props.startTime}>
        <Timer
            initialTime={props.startTime}
            lastUnit="m"
            startImmediately={false}
            formatValue={(value) => `${(value < 10 ? `0${value}` : value)}`}
        >
            {(control) => {
                stopwatchControl.current = control
                return (
                    <React.Fragment>
                        <Timer.Minutes />:
                        <Timer.Seconds />
                    </React.Fragment>
                )
            }}
        </Timer>
    </div>
);

}

export default TimerComponent; `

in the parent component: ` const [timerData, setTimerData] = useState<ITimer>({ command: "stop", startTime: 0 });

useEffect(() => {
    setTimerData({ command: "start", startTime: 60000 });// 1 minute
}, []);

return (
       <TimerComponent command={timerData.command} startTime={timerData.startTime} />
);

} `

Hila1 avatar Mar 16 '21 12:03 Hila1