<div id="root"></div>
const App = () => {
    const [count, setCount] = React.useState(0)
    const [text, setText] = React.useState('')
    const [isShow, setShow] = React.useState(true)

    // レンダリングされた時にのみ実行
    React.useEffect(() => {
        console.log(`レンダリングされました。`)
    }, [])

    // count または textが更新された時に実行
    React.useEffect(() => {
        console.log(`count または textが更新されました。count=${count} text=${text}`)
    }, [text, count])

    // コンポーネントが削除される際に実行
    React.useEffect(() => {
        return () => {
            console.log('コンポーネントが削除されました。')
        };
    }, [])

    return (
        <div>
            <input type='text' value={text} onChange={e => setText(e.target.value)}></input>
            <button onClick={() => setCount(count + 1)}>クリック {count} 回</button>
        </div>
    )
}

ReactDOM.render(<App />, document.getElementById("root"));
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/react/16.7.0-alpha.2/umd/react.production.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.7.0-alpha.2/umd/react-dom.production.min.js