Skip to content

Instantly share code, notes, and snippets.

@alqamabinsadiq
Created December 15, 2018 18:14
Show Gist options
  • Save alqamabinsadiq/199c69ef26d066181ace2792037911c7 to your computer and use it in GitHub Desktop.
Save alqamabinsadiq/199c69ef26d066181ace2792037911c7 to your computer and use it in GitHub Desktop.
Code From Getting Started With Hooks - ReactKHI Meetup #02
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