Created
September 9, 2020 08:30
-
-
Save anechunaev/5544f5afa4dba59523d5806347154f81 to your computer and use it in GitHub Desktop.
Custom input-like event in React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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