title: Pass a Variable Reference In React?
body: <p>I am designing a react component, and am trying to reduce the codebase and remove code duplication. </p>
  
  <p>One thing that has a lot of code duplication is the checking input in the setter variables. Much of these setters do the same checking before they actually set the variable. Now, I know I could abstract that checking to a function to reduce duplication. But I was wondering, is there anyway to take it a step further, and reduce it all down to a single function? </p>
  
  <p>For Example: </p>
  
  <pre><code>setVariable1(e){
    // Input validation
    // Rewrite Variable1
  }
  
  setVariable2(e){
    // Input Validation
    // Rewrite Variable2
  }
  
  &lt;input onChange={this.setVariable1} type="number" value={this.variable2} className="form-control" required/&gt;
  
  &lt;input onChange={this.setVariable2} type="number" value={this.variable2} className="form-control" required/&gt;
  </code></pre>
  
  <p>Now if I used an arrow function, or perhaps currying to pass the variable into the function. Would there be a way to pass the reference of the variable in, instead of the value? Sort of like how you can add the &amp; in C to affect the address? </p>
  
  <pre><code>setVariableType1(e, var){
      // Input Validation
      // Set variable var
  }
  
  &lt;input onChange={(e) =&gt; this.setVariableType1(this.variable1)} type="number"  value={this.variable2} className="form-control" required/&gt;
  
  &lt;input onChange={(e) =&gt; this.setVariableType1(this.variable2} type="number"  value={this.variable2} className="form-control" required/&gt;
  </code></pre>
  
  <p>However, I notice when I do it this way. Instead of passing the variable into the function, I just pass the variable's contents in. So is there a way to pass in the reference?</p>
  
  <p>I am decently new to React and still learning the ins and outs, so maybe there is a completely different way to do this using the component hierarchy with props and states etc? I just can't seem to figure out the best way to do this, or even if it's proper to want to do this. </p>
  
  <p>Any help would be appreciated, thanks! </p>