This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default function useData(initData) { | |
| const [data, setData] = useState(initData); | |
| useEffect(() => { | |
| const intervalId = setInterval(async () => { | |
| const newData = await fetch('https://data-api.com/getData').then(response => response.json()); | |
| setData(newData); | |
| }, 50000); | |
| return () => { | |
| clearInterval(intervalId); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class StoppableClock extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| time: new Date().toLocaleTimeString(), | |
| isRunning: true, | |
| }; | |
| } | |
| componentDidMount() { | |
| this.intervalId = setInterval(() => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class StoppableClock extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| time: new Date().toLocaleTimeString(), | |
| isRunning: true, | |
| }; | |
| } | |
| componentDidMount() { | |
| this.intervalId = setInterval(() => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function StoppableClock({ className, style }) { | |
| const [time, setTime] = useState(new Date().toLocaleTimeString()); | |
| const [isRunning, setIsRunning] = useState(true); | |
| const toggle = useCallback(() => { | |
| setIsRunning(isRunning => !isRunning); | |
| }, []); | |
| useEffect(() => { | |
| if (isRunning) { | |
| const intervalId = setInterval(() => { | |
| setTime(new Date().toLocaleTimeString()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Clock({ className, style }) { | |
| const [time, setTime] = useState(new Date().toLocaleTimeString()); | |
| useEffect(() => { | |
| setInterval(() => { | |
| setTime(new Date().toLocaleTimeString()); | |
| }, 1000); | |
| }, []); | |
| return ( | |
| <span | |
| className={clsx('Clock', className)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Clock extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| time: new Date().toLocaleTimeString(), | |
| }; | |
| } | |
| componentDidMount() { | |
| setInterval(() => { | |
| this.setState({ time: new Date().toLocaleTimeString() }); |
NewerOlder