Home > Mobile >  What type is passed to functions from `onClick`?
What type is passed to functions from `onClick`?

Time:01-20

I have some javascript that looks like this:

const menu = (
        <Menu onClick={handleMenuClick}>
            {menuItems.map((item) => (
                <Menu.Item key={item.key} icon={item.icon} style={styles.item}>
                    <span style={{ marginLeft: "5px" }}>{item.value}</span>
                </Menu.Item>
            ))}
        </Menu>
    )

With the key piece being the handleMenuClick

That function is defined as:

    const handleMenuClick = (event) => {
        console.log(event.key)
        console.log(typeof (event.key))
    }

However, I'm not sure what type the event is, and how it's getting the key value?

Additionally, when I try this in typescript, it looks like its a enter image description here

Then copy that type when defining a standalone callback that you can pass in.

This works not only for onClick in React, but for any situation where you're dealing with a callback you don't know the proper type for.

  •  Tags:  
  • Related