Skip to content

Commit

Permalink
Effects with Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Oct 25, 2022
1 parent eb928ed commit 407cd8e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 13 additions & 1 deletion react-hooks/home/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Gallery from './Gallery';

function App() {
const [userQuery, setUserQuery] = useState('');
const [showGallery, setShowGallery] = useState(true);

const updateUserQuery = event => {
console.log('userQuery', userQuery);
Expand All @@ -23,6 +24,10 @@ function App() {
window.open(`https://google.com/search?q=${userQuery}`, '_blank');
}

const toggleShowGallery = () => {
setShowGallery(!showGallery);
}

return (
<div className="App">
<h1>Hello David</h1>
Expand All @@ -39,7 +44,14 @@ function App() {
<hr />
<Tasks />
<hr />
<Gallery />
<div>
{
showGallery ? <Gallery /> : null
}
<button onClick={toggleShowGallery}>
{showGallery ? 'Hide' : 'Show'} Gallery
</button>
</div>
<hr />
<Stories />
</div>
Expand Down
6 changes: 5 additions & 1 deletion react-hooks/home/src/Gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ function Gallery() {
const [index, setIndex] = useState(0);

useEffect(() => {
setInterval(() => {
const interval = setInterval(() => {
setIndex(
storedIndex => {
return (storedIndex+1)%PICTURES.length;
}
);
}, 3000);

return () => {
clearInterval(interval);
}
}, []);

console.log('index', index);
Expand Down

0 comments on commit 407cd8e

Please sign in to comment.