Correct typing for your Hamburger functional component is:

function Hamburger({ onClick }: { onClick? : React.MouseEventHandler }): JSX.Element {
    // actual code
}

As number of props grow, inline type declarations may get messy. So, it's a good habit to move them into designated type:


type Props = {
    onClick?: React.MouseEventHandler
}

function Hamburger({ onClick }: Props) JSX.Element {
    // actual code
}

It also has benefits if later you'll have to accept children prop in this component. Then you may use React.FC helper with Props type:

const Hamburger: React.FC<Props> = ({ onClick, children }) => { // no need to type `children` prop yourself
    // actual code
}
Answer from aleksxor on Stack Overflow
๐ŸŒ
xjavascript
xjavascript.com โ€บ blog โ€บ pass-onclick-as-props-react-typescript
Passing `onClick` as Props in React with TypeScript โ€” xjavascript.com
By passing `onClick` handlers as props, parent components can control the behavior of child components when a click event occurs. This blog post will explore the fundamental concepts, usage methods, common practices, and best practices of passing ...
Discussions

Best way to pass onClick, children, and similar ReactNode attributes as props to component?
If you're OK inheriting every HTML props, you can do something like type ProductCardProps = HTMLProps If you want to limit to some of them, something like this should work: type ProductCardProps = Pick>, 'onClick' | 'children'> & IProductCardDataModel There's probably a more elegant solution but that's more or less what I would do. More on reddit.com
๐ŸŒ r/reactjs
7
4
June 26, 2023
javascript - React Typescript: Passing onClick as a Prop
I am trying to create a simple Hamburger Menu component using React & Typescript. What I want to do is to pass the onClick event handler to this menu componunent as a prop. This is the code t... More on stackoverflow.com
๐ŸŒ stackoverflow.com
React passing onClick as props to sub components required?
onClick is an event in button element while onClick is props in Linkbutton component. ... Unfortuantely, there is no way around that in pure React.js. If you want the parent element's function to handle click event performed on a child element, you have to explicitly pass that function as a ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Help passing object through onClick of component.

You could try onClick={() => props.soundDisplayChange(x.sound)}

More on reddit.com
๐ŸŒ r/reactjs
12
4
September 5, 2018
๐ŸŒ
React
legacy.reactjs.org โ€บ docs โ€บ faq-functions.html
Passing Functions to Components โ€“ React
You can use an arrow function to wrap around an event handler and pass parameters: <button onClick={() => this.handleClick(id)} /> This is equivalent to calling .bind: <button onClick={this.handleClick.bind(this, id)} /> const A = 65 // ASCII ...
๐ŸŒ
Reddit
reddit.com โ€บ r/reactjs โ€บ best way to pass onclick, children, and similar reactnode attributes as props to component?
r/reactjs on Reddit: Best way to pass onClick, children, and similar ReactNode attributes as props to component?
June 26, 2023 -

I am currently working on creating a card component with various interactions. I want to have an interface to represent the data model of the card, but I don't want to have to add dummy values for onClick and such.

Is the following an acceptable approach? Or is there a much better way to do this? Thank you.
https://ibb.co/7RmKRC1

๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ react-typescript-pass-function-as-prop
How to pass Functions as Props in React TypeScript | bobbyhadz
February 29, 2024 - If you need to pass a function as props in a React application that doesn't use TypeScript, check out the following tutorial. A common thing you might have to do is pass an event handler function as props.
Find elsewhere
๐ŸŒ
CopyProgramming
copyprogramming.com โ€บ howto โ€บ typescript-set-type-for-onclick-in-typescript-react
Reactjs: Setting TypeScript type for onclick in React with TypeScript
July 27, 2023 - Refining event type for onClick using Typescript and React.Konva, Using onClick as a Prop in React Typescript, React Link / href - IntrinsicAttributes & LinkProps Type Does Not Include 'onClick' Property, Passing an onClick function as props to a React component using Typescript: A
๐ŸŒ
React
react.dev โ€บ learn โ€บ responding-to-events
Responding to Events โ€“ React
UploadButton passes () => alert('Uploading!') as the onClick prop to the Button inside.
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ react onclick event handlers: a complete guide
React onClick event handlers: A complete guide - LogRocket Blog
December 10, 2024 - So how do we handle event handling for custom components in React? By rendering a DOM element inside the CustomButton component and passing the onClick prop into it.
๐ŸŒ
Webdevtutor
webdevtutor.net โ€บ blog โ€บ typescript-react-onclick-props
Using TypeScript with React: Handling onClick Props
import React from 'react'; import ... <Button onClick={handleClick} />; }; export default App; In this snippet, we define a handleClick function that logs a message when the button is clicked....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ react: help understanding the onclick prop differences?
r/learnjavascript on Reddit: React: Help understanding the onClick prop differences?
June 15, 2021 -

Hello all, I went through and completed this tutorial, a tic tac toe game, in which the final state of the code can be found here. What I don't understand are the differences in the onClick functions when we're creating the game, board, and square. For example:

  1. Within the Game's render method, when we create the board we pass the prop "onClick={i => this.handClick(i)}". When the board is clicked, where is this parameter i coming from? As in, what is supplying it?

  2. Within the Board's renderSquare method, when we create the Square, the onClick prop we supply is "{() => this.props.onClick(i)}", why does it not take a parameter now?

  3. Lastly, within the Square function, we return a square whose onClick looks very different from the others, simply "props.onClick". The tutorial mentions "note the missing parenthesis" but doesn't explain why.

So basically I'd like to understand why in some cases we use "(i) =>" and in others we use "() =>" and finally, why sometimes we don't use arrow functions at all.

Top answer
1 of 2
1
I don't know React specifically, but in Javascript, all event handlers are passed an event object as their first argument. These can be useful if you want to get specific information about the click event (position of the click, element that was clicked etc). When we modified the Square to be a function component, we also changed onClick={() => this.props.onClick()} to a shorter onClick={props.onClick} (note the lack of parentheses on both sides). This is because there is no functional difference in this case between onClick={() => this.props.onClick()} and onClick="{props.onClick}. In the former case, you're passing an anonymous function that simply calls props.onClick' whereas in the latter you're passing the props.onClick function itself. Both are functions that the onClick handler uses as a callback when it detect a click.
2 of 2
1
The component in is receiving a clickHandler, and it doesn't care about what parameters might be passed into it. The component will fire a callback on being clicked, and will pass it a ReactSyntheticEvent object. In this case, the develop is omitting the () => ... portion because it isn't necessary. They don't care about what arguments might be passed in. The props.onClick function being passed into from is the function () => this.props.onClick(i). This function ignores any arguments you might try and pass to it, so the ReactSyntheticEvent object is just discarded. Each square receives a function with the value of i embedded in it, and when the square is clicked, it invokes that function. The value of i comes from the parameter of Board.renderSquare(i) { ... }, which is provided with each invocation: {this.renderSquare(0)} {this.renderSquare(1)} {this.renderSquare(2)} in turn is receiving an onClick prop from onClick={i => this.handleClick(i)} Here they're omitting the parenthesis because they aren't necessary for an arrow function with a single parameter. They could also write onClick={this.handleClick} and not use an arrow function. Ultimately what is happening here is each component is passed a function as a prop. That component then wraps it in another function and passes that down in turn: (i) => this.handleClick(i) () => this.props.onClick(i) props.onClick We can rewrite this as: props.onClick = () => this.props.onClick(i) this.props.onClick = (i) => this.handleClick(i) So props.onClick is really () => ((i) => this.handleClick(i))(i) // () => IIFE
๐ŸŒ
React TypeScript Cheatsheets
react-typescript-cheatsheet.netlify.app โ€บ useful patterns by use case
Useful Patterns by Use Case | React TypeScript Cheatsheets
As you can see from the Omit example above, you can write significant logic in your types as well. type-zoo is a nice toolkit of operators you may wish to check out (includes Omit), as well as utility-types (especially for those migrating from Flow). Sometimes you want the prop types of a component, but it isn't exported. ... // a Modal component defined elsewhere const defaultProps: React.ComponentProps<typeof Modal> = { title: "Hello World", visible: true, onClick: jest.fn(), };
๐ŸŒ
Medium
medium.com โ€บ @batahumphrey66 โ€บ how-to-pass-a-value-to-the-onclick-callback-in-a-react-component-aa354b04de4c
How to Pass a Value to the onClick Callback in a React Component | by Batahumphrey | Medium
August 8, 2023 - You can also call a component from the onClick callback and pass the value as a prop to the component. To do this, you create a function that returns a component, and then pass that function from the onClick prop.
๐ŸŒ
Codemzy
codemzy.com โ€บ blog โ€บ react-pass-function-as-prop
3 ways to pass a function as a prop in React - Codemzy's Blog
September 15, 2023 - For example, one button might submit ... you get the idea! ... All of the props we send to our Button component are given to the button, with{...props}, so we can pass a function for the onClick handler....