2

I'm using a trash can icon in a react-table row, and I want to delete the row when the icon is clicked (my knowledge of HTML/JS is very basic). But when I put an onClick handler either in or around the icon, it fires as the page is rendered, and not at all when the icon is clicked.

Here is my current row definition. I've tried div and span, and I tried using a button, but the icon didn't display correctly:

{
  width: 50,
  filterable: false,
  Cell: row => (
  <div align="center" onClick={alert("clicked")}>
    <i className="fa fa-trash-o"></i>
  </div>
  )
}

Can someone please tell me what I'm doing wrong?

2
  • Possible duplicate of React onClick event Commented May 1, 2018 at 22:28
  • You should also probably be using a button instead of a div Commented May 1, 2018 at 22:38

3 Answers 3

7

React event bindings do not work without an actual function wrapper.

Try it as onClick={()=>{alert('clicked')}}

See also: these docs.

Sign up to request clarification or add additional context in comments.

2 Comments

Fastest ever answer! But it doesn't work... I get a syntax error: Adjacent elements must be wrapped in an enclosing tag.
A right bracket was missing. Works great now. Thanks!
0

You certainly can make icons clickable. You could go one of two routes I can think of atm..

You can either surround the icon in an 'a' tag or you could use javascript to grab the element (probably getElementById) and add a click listener to it, with an easy function attached that executes your hearts desire.

Hope that helps

1 Comment

I'm sorry, I don't have the experience to even understand what you wrote! Can you write an example?
0

You don't need the brackets.

You can just have the function as alert('clicked') or you can define a function inside of your JavaScript and link to it in the onclick of the span.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.