Eugene Podoliako Eugene Podoliako - 1 month ago 6
React JSX Question

React transfer event into onClick

I have onClick event that excute fuction with several params. How to add to this paramt current event? I want to do in function somethig like this

event.stopPropagation()


My code has the following view (all not nesseccary code was removed):

import React from 'react';

export default class AllCarWashTable extends React.Component{

constructor(props) {
super(props);
this.removeCarWashHandler = this.removeCarWashHandler.bind(this);
...
};

removeCarWashHandler(e, name, id){
e.stopPropagation();
...
}

generateRows() {
const removeCarWashHandler = this.removeCarWashHandler
...

return data.map(function(item) {
var cells = cols.map(function(colData) {
...
<button type="button"
onClick={()=> removeCarWashHandler(item['name'], item['id'])}>
ButtonName
</button>
...
});

}
}


Currenly my code leads to exception:
TypeError: Cannot read property 'stopPropagation' of undefined

How to fix this?

Answer

You forgot to pass the event to the function, it can be done giving the onClick event to the removeCarWashHandler function:

onClick={e => removeCarWashHandler(e, item['name'], item['id'])}