Quellcode durchsuchen

Handling copy game ID logic w/ proper React ref

Kirk Trombley vor 5 Jahren
Ursprung
Commit
09568b4152
1 geänderte Dateien mit 9 neuen und 5 gelöschten Zeilen
  1. 9 5
      client/src/components/pre-round.component.jsx

+ 9 - 5
client/src/components/pre-round.component.jsx

@@ -1,11 +1,14 @@
 import React from "react";
 
 class PreRound extends React.Component {
+  constructor(props) {
+    super(props);
+    this.textareaRef = React.createRef();
+  }
+
   copyGameId() {
-    const { gameId } = this.props;
-    const textarea = this.textarea;
-    textarea.value = gameId;
-    textarea.select();
+    const { current } = this.textareaRef;
+    current.select();
     document.execCommand("copy");
   }
   
@@ -19,8 +22,9 @@ class PreRound extends React.Component {
         </p>
         <button onClick={onStart}>Start Game</button>
         <textarea
-          ref={textarea => this.textarea = textarea}
+          ref={this.textareaRef}
           style={{ position: "absolute", top: "-1000px" }}
+          value={gameId}
         />
       </div>
       );