Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active April 3, 2019 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanflorence/850191d717219e8ae505ef69502f5ff3 to your computer and use it in GitHub Desktop.
Save ryanflorence/850191d717219e8ae505ef69502f5ff3 to your computer and use it in GitHub Desktop.
import React, { useState } from "react"
function UiButton({ onToggle, isActive: isActiveProp, ...props }) {
const [isActive, setIsActive] = useState(isActiveProp)
const isControlled = isActive != null
const clickHandler = isControlled
? onToggle
: () => {
onToggle()
setIsActive(!isActive)
}
return (
<button
style={{
color: isActive ? "blue" : "red"
}}
onClick={clickHandler}
/>
)
}
function Tab({ isActive, onSelect, ...props }) {
return <UiButton isActive={isActive} onToggle={onSelect} {...props} />
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment