React js onClick can't pass value to method

后端 未结 30 1735
北荒
北荒 2020-11-22 07:05

I want to read the onClick event value properties. But when I click on it, I see something like this on the console:

SyntheticMouseEvent {dispatchConfig: Ob         


        
30条回答
  •  花落未央
    2020-11-22 07:16

    There were a lot of performance considerations, all in the vacuum.
    The issue with this handlers is that you need to curry them in order to incorporate the argument that you can't name in the props.
    This means that the component needs a handler for each and every clickable element. Let's agree that for a few buttons this is not an issue, right?
    The problem arises when you are handling tabular data with dozens of columns and thousands of rows. There you notice the impact of creating that many handlers.

    The fact is, I only need one.
    I set the handler at the table level (or UL or OL...), and when the click happens I can tell which was the clicked cell using data available since ever in the event object:

    nativeEvent.target.tagName
    nativeEvent.target.parentElement.tagName 
    nativeEvent.target.parentElement.rowIndex
    nativeEvent.target.cellIndex
    nativeEvent.target.textContent
    

    I use the tagname fields to check that the click happened in a valid element, for example ignore clicks in THs ot footers.
    The rowIndex and cellIndex give the exact location of the clicked cell.
    Textcontent is the text of the clicked cell.

    This way I don't need to pass the cell's data to the handler, it can self-service it.
    If I needed more data, data that is not to be displayed, I can use the dataset attribute, or hidden elements.
    With some simple DOM navigation it's all at hand.
    This has been used in HTML since ever, since PCs were much easier to bog.

提交回复
热议问题