Skip to content

[TypeScript] Bind event function to another function

Thang Chung edited this page Mar 20, 2019 · 2 revisions
interface SearchPriceProps {
  onPriceChange: (event: React.ChangeEvent<HTMLInputElement>) => void
  price: number
}
const SearchPrice = (Props: SearchPriceProps) => {
  return (
    <div>
      Price:
      <input type="text" value={Props.price} onChange={Props.onPriceChange} />
    </div>
  )
}
// react hook
const [price, setPrice] = useState(999)

And in the code

<SearchPrice
	price={price}
    onPriceChange={e => {
    	setPrice(parseInt(e.target.value))
    }}
/>

#ref https://www.pluralsight.com/guides/typescript-pass-function-react