Skip to content

Instantly share code, notes, and snippets.

@anechunaev
Created September 9, 2020 08:30
Show Gist options
  • Save anechunaev/5544f5afa4dba59523d5806347154f81 to your computer and use it in GitHub Desktop.
Save anechunaev/5544f5afa4dba59523d5806347154f81 to your computer and use it in GitHub Desktop.
Custom input-like event in React
const root = document.querySelector('#root');
const customEvent = new CustomEvent(
"myEvent",
{
bubbles: false,
cancelable: false,
detail: null,
},
);
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return React.createElement("button", {
ref: this.myRef,
onClick: (e) => {
Object.defineProperty(this.myRef.current || {}, "value", { value: "111" });
Object.defineProperty(customEvent, "target", { value: this.myRef.current });
Object.defineProperty(customEvent, "currentTarget", { value: this.myRef.current });
console.log(customEvent.currentTarget.value);
console.log(customEvent.currentTarget);
console.log(customEvent);
},
}, "Click me!");
}
}
ReactDOM.render(React.createElement(MyComponent), root);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment