Created
December 15, 2018 18:14
-
-
Save alqamabinsadiq/199c69ef26d066181ace2792037911c7 to your computer and use it in GitHub Desktop.
Code From Getting Started With Hooks - ReactKHI Meetup #02
This file contains 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
import React, { useState, useEffect } from 'react'; | |
const Counter = () => { | |
// initial state. | |
const initialState = { | |
counter: 0 | |
}; | |
const [state, setState] = useState(initialState); | |
// increament the counter | |
const increament = () => { | |
setState({ | |
counter: state.counter + 1 | |
}) | |
} | |
useEffect(() => { | |
// Update the document title using the browser API | |
document.title = `You clicked ${state.counter} times`; | |
}); | |
// sets the interval | |
useEffect(() => { | |
const timerID = setInterval(() => { | |
increament(); | |
}, 1000); | |
return () => clearInterval(timerID); // cleaup function | |
}); | |
return ( | |
<div> | |
{state.counter} | |
</div> | |
); | |
} | |
export default Counter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment