1const Enter = () => {
2 const handle = () => console.log('Enter pressed');
3
4 return <input type="text" onKeyDown={e => e.key === 'Enter' && handle} />;
5};
1var Input = React.createClass({
2 render: function () {
3 return <input type="text" onKeyDown={this._handleKeyDown} />;
4 },
5 _handleKeyDown: function(e) {
6 if (e.key === 'Enter') {
7 console.log('do validate');
8 }
9 }
10});
11
1 useEffect(() => {
2 const listener = event => {
3 if (event.code === "Enter" || event.code === "NumpadEnter") {
4 console.log("Enter key was pressed. Run your function.");
5 event.preventDefault();
6 // callMyFunction();
7 }
8 };
9 document.addEventListener("keydown", listener);
10 return () => {
11 document.removeEventListener("keydown", listener);
12 };
13 }, []);
14