Unexpected use of 'event' no-restricted-globals when using event.target.id to get id from bind(this)
It's strange that this even works in codepen -- it looks like you're using a global event
property.
The right way to do this is to get the event object from the handleClick
function's first param:
handleClick(event) {
var clickedId = event.target.id;
console.log(clickedId);
alert("It works! You clicked " + clickedId)
}
You might need to specify the event on the passing function I guess.
const Elem = (props) =>{
return (<div>
<h1 onClick={props.clickon} id="GM"> Good Morning!
<br/>
{props.name} {props.last}
<br />
This is phase three</h1>
<button id="btn1" onClick={props.clickon}> {props.text} </button>
<button id="btn2" onClick={props.clickon}> Second Button </button>
</div>
);
};
class App extends React.Component{
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick(event){
var clickedId = event.target.id;
console.log(clickedId);
alert("It works! You clicked " + clickedId)
}
render(){
return (
<Elem name = 'paul' last='shreeman' clickon={(event)=>this.handleClick(event)} text='PushMe'/>
)
}
}
ReactDOM.render(
<App />, document.getElementById('root'))
Try this if it works for you.