Skip to content

Instantly share code, notes, and snippets.

@beardlessman
Created September 4, 2019 06:50
Show Gist options
  • Save beardlessman/20df1952da3b233805f9fffe45665e23 to your computer and use it in GitHub Desktop.
Save beardlessman/20df1952da3b233805f9fffe45665e23 to your computer and use it in GitHub Desktop.
react hook example, flow
//@flow
import React, { useState, useEffect } from 'react';
type IProps = {
initial?: number,
};
export default function HookExample({ initial = 0 }: IProps) {
const [fuckingCount, setFuckingCount] = useState(initial);
const [kek, setKek] = useState('kek');
useEffect(() => {
document.title = `You clicked ${fuckingCount} times. ${kek}?`;
return () => console.log('UNMOUNT HookExample', kek); // выполнится перед unmount компонента
}, [kek]); // если изменилось хотя бы одна сущность из массива -- выполняем эффект
return (
<div>
<p>You clicked {fuckingCount} fucking times</p>
<button onClick={() => setFuckingCount(fuckingCount + 1)}>Click me</button>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment