Skip to content

Instantly share code, notes, and snippets.

@Patrick-DS
Last active October 31, 2020 21:02
Show Gist options
  • Save Patrick-DS/50a9132b82c456cf8f7fb5708ded664e to your computer and use it in GitHub Desktop.
Save Patrick-DS/50a9132b82c456cf8f7fb5708ded664e to your computer and use it in GitHub Desktop.
// CountingButton.js
import React, { useState, useEffect } from "react"
const CountingButton = ({ onClick, count }) => {
const [displayText, setDisplayText] = useState("")
useEffect(() => {
setDisplayText(`You clicked me ${count} times!`)
}, [count, setDisplayText])
return <button onClick={onClick}>{displayText}</button>
}
export default CountingButton
// someOtherFile.js
import React, { useState, useEffect } from "react"
import CountingButton from "./CountingButton"
const ExampleComponent = () => {
const [count, setCount] = useState(0)
const increaseCount = () => setCount(count+1)
return <CountingButton onClick={increaseCount} count={count} />
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment