Last active
July 24, 2022 18:56
-
-
Save egor-xyz/5103e5ac980375325f4442c117e40074 to your computer and use it in GitHub Desktop.
Medium. Custom react hooks with JSX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useState } from "react"; | |
import { SmallModal } from "./components/SmallModal"; | |
export const AnotherComponent = () => { | |
// same code again | |
const [isOpen, setIsOpen] = useState(false); | |
// same code again | |
const toggleOpen = useCallback(() => { | |
setIsOpen((isOpen) => !isOpen); | |
}, []); | |
return ( | |
<> | |
<SmallModal | |
isOpen={isOpen} | |
text='Second' | |
/> | |
<div onClick={toggleOpen}>Show bill</button> | |
</> | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useState } from "react"; | |
import { SmallModal } from "./components/SmallModal"; | |
export const Header = () => { | |
const [isOpen, setIsOpen] = useState(false); | |
const toggleOpen = useCallback(() => { | |
setIsOpen((isOpen) => !isOpen); | |
}, []); | |
return ( | |
<> | |
<button onClick={toggleOpen}>Show user</button> | |
<SmallModal | |
isOpen={isOpen} | |
text='First' | |
/> | |
</> | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useSmallModal } from "./components/SmallModal"; | |
export const AnotherComponent = () => { | |
const { SmallModal, toggle } = useSmallModal(true); | |
return ( | |
<> | |
<SmallModal text='Second' /> | |
<div onClick={toggle}>Show bill</button> | |
</> | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useSmallModal } from "./components/SmallModal"; | |
export const Header = () => { | |
const { SmallModal, toggle } = useSmallModal(true); | |
return ( | |
<> | |
<button onClick={toggle}>Show user</button> | |
<SmallModal text='First' /> | |
</> | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useState } from "react"; | |
import { SmallModal } from "./components/SmallModal"; | |
export const useSmallModal: UseSmallModal = (defIsOpen = false) => { | |
const [isOpen, setIsOpen] = useState(defIsOpen); | |
const toggle = useCallback(() => { | |
setIsOpen(state => !state) | |
}, []); | |
const Component = useCallback(({ text }) => ( | |
<SmallModal | |
text={text} | |
isOpen={isOpen} | |
/> | |
), [isOpen]); | |
return { SmallModal: Component, toggle }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment