简体   繁体   中英

Unable to pass parameter to React Button callback function on click

I have a material-ui button and am trying to pass some parameter to my callback function on click.

For some reason, the below way is not working.

const myApi = async (param) => {
}

<Button onClick={myApi('text')}
    data-testid='my-field'>
    <Icon accessibleText="__label__search" name="search" size={12} />
</Button>

Use

 onClick = {() => myApi("text")}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM