Skip to content

Instantly share code, notes, and snippets.

@ak1103dev
Last active May 5, 2019 14:53
Show Gist options
  • Save ak1103dev/098388db16c33e5384267c280f070b4e to your computer and use it in GitHub Desktop.
Save ak1103dev/098388db16c33e5384267c280f070b4e to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
import Hook from './Hook'
import './App.css';
function App() {
const [isLifecycle, setIsLifecycle] = useState(false)
return (
<div className="App">
<button onClick={() => setIsLifecycle(!isLifecycle)}>
Switch
</button>
{
isLifecycle ? null : <Hook />
}
</div>
);
}
export default App;
import React, { useState, useEffect } from 'react'
function Hook() {
const [count, setCount] = useState(0)
useEffect(() => {
console.log('Hook: use effect')
document.title = `${count} times`;
return function cleanup() {
console.log('Hook: cleanup')
}
});
console.log('Hook: before render')
return (
<div>
<h1>Hook</h1>
<h2>{count}</h2>
<button onClick={() => setCount(count + 1)}>Add</button>
</div>
)
}
export default Hook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment