How do I pass an ID using a button in React?

react onclick get element id
react onclick pass props
react onclick pass event and parameter
react onclick pass parameter
react pass parameter on button click
react onclick pass input value
addeventlistener react
react onclick event target

This is my component. I'm trying to make a delete button that sends a DELETE request to the server. What I'm struggling with is, how do I pass the ID of the post to handleDelete()?

import React from 'react';
import client from '../../api/';
import { Button } from 'reactstrap'
class Post extends React.Component {

  constructor(props) {
    super(props) =

  handleDelete(event) {

    render() {
      return (
          <h1>Title: {this.props.title} {}</h1>
          <Button id={} onClick={this.handleDelete} color="danger">Delete</Button>

export default Post

This should do the trick:

constructor(props) {
  this.handleDelete = this.handleDelete.bind(this)

handleDelete(event) {

pass id through on click react.js, You can do this as follows : class Test extends React. Component { constructor(props){ super(props); this. state = { items: [ {item: "item", id: 1}, {item1: "item1", id: 2} ] } } handleClick(id, e){ alert(id); } render(){ return( <ul id="todo"> {this. I now recommend using the react-html-id npm module to solve this problem instead. It works very similarily to the older `unique-id-mixin`. It works very similarily to the older `unique-id-mixin`. I’ve recently been using React as a framework for more advanced web development, and I love it, more about that in a future post.

Use and rewrite handleDelete function with arrow function to save components context

handleDelete=(event) => {
    alert(;//here is post id

Pass a Parameter Through onClick in React, It's a single button with one event handler: onClick. Typically, to call a function when we click a button in React, we would simply pass in the name  Imagine a scenario where you have a group of three buttons. When you click on one of them, you want to know which one was clicked and perform an appropriate action. You have a choice of creating three onClick event handlers, one for each button, or one hander for all the buttons and pass a parameter identifying the clicked button into it.

Pass a function to the onClick handler

<Button id={} onClick={() => this.handleDelete(} color="danger">Delete</Button>

How to pass a value to onClick event handler in React.js, How to pass values to onClick event handler in React via data attributes. for all the buttons and pass a parameter identifying the clicked button into it. from '​div_id' // and 'div_name' datasets message: `Clicked div Id ${ev. The simple App component above has one function called sayHello(), and a single button. The button inside the React component has an onClick event handler attached to it, pointing to our sayHello() function. Doing so will trigger the function every time you click the button.

Handling Events – React, With JSX you pass a function as the event handler, rather than a string. For example, the HTML: <button onclick="activateLasers()"> Activate Lasers </button​> For example, if id is the row ID, either of the following would work: <button  With React, typically you only need to bind the methods you pass to other components. For example, <button onClick={this.handleClick}> passes this.handleClick so you want to bind it. However, it is unnecessary to bind the render method or the lifecycle methods: we don’t pass them to other components.

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  A basic button component that should render nicely on any platform. Supports a minimal level of customization. If this button doesn't look right for your app, you can build your own button using TouchableOpacity or TouchableNativeFeedback. For inspiration, look at the source code for this button component.

Route Params ―, With react-router-dom , we designate a dynamic portion of the URL to be This is because I have hardcoded the ID value that is being queried from the API in UserInfo . being passed into our route and use it to query the correct user from the API. Import Link and wrap the button in src/fe/components/UserInfo.js like so. When using React you should generally not need to call addEventListener to add listeners to a DOM element after it is created. Instead, just provide a listener when the element is initially rendered. When you define a component using an ES6 class, a common pattern is for an event handler to be a method on the class.