title: Set the value of a form based on state in React
body: <p>Using Formik for validating some fields, if a state value is true I want that the value of a fields to be copied to the value of another one.</p>
  <p>For example, value of <code>mainAddress</code> to be copied to <code>address</code>.</p>
  <p>There is a state variable, <code>setAddress</code> which is set on false but it can be changed to true when a checkbox is clicked.</p>
  <p>When this variable is set on true I want that the value of <code>mainAddress</code> to be copied to <code>address</code></p>
  <p>This is the code that works fine without copying that value:</p>
  <pre><code>import React from 'react';
  import { Formik, Form, Field } from 'formik';
  import { Input, Button, Label, Grid } from 'semantic-ui-react';
  import * as Yup from 'yup';
  import { Creators } from '../../../actions';
  import './CreateCompanyForm.scss';
  
  class CreateCompanyForm extends React.PureComponent {
    constructor(props) {
      super(props);
  
      this.state = { // state variables 
        address: '',
        mainAddress: '',
        setAddress: false,
      };
    }
  
    handleSubmit = values =&gt; {
      // create company
    };
  
    toggleAddress = () =&gt; { // it toggles the value of setAddress
      this.setState(prevState =&gt; ({
        setAddress: !prevState.setAddress,
      }));
    };
  
    render() {
      const initialValues = { // the address and mainAddress are set to '' at the start
        address: '',
        mainAddress: '',
      };
      const validationSchema = Yup.object({
        address: Yup.string().required('error'),
        mainAddress: Yup.string().required('error'),
      });
      return (
        &lt;&gt;
          &lt;Button type=&quot;submit&quot; form=&quot;amazing&quot;&gt; // button for submit
            Create company
          &lt;/Button&gt;
  
          &lt;Formik
            htmlFor=&quot;amazing&quot;
            initialValues={initialValues}
            validationSchema={validationSchema}
            onSubmit={values =&gt; this.handleSubmit(values)}&gt;
            {({ values, errors, touched, setValues, setFieldValue }) =&gt; (
              &lt;Form id=&quot;amazing&quot;&gt;
                &lt;Grid&gt;
                  &lt;Grid.Column&gt; // mainAddress value
                    &lt;Label&gt;Main Address&lt;/Label&gt;
                    &lt;Field name=&quot;mainAddress&quot; as={Input} /&gt;
                  &lt;/Grid.Column&gt;
  
                  &lt;Grid.Column&gt; // address value
                    &lt;Label&gt;Address&lt;/Label&gt;
                    &lt;Field name=&quot;address&quot; as={Input} /&gt;
                    &lt;div&gt;
                      {this.state.setAddress // here is how I've tried to set that value
                        ? values.address.setFieldValue(values.mainAddress)
                        : console.log('nope')}
                      {touched.address &amp;&amp; errors.address ? errors.address : null}
                    &lt;/div&gt;
                  &lt;/Grid.Column&gt;
                &lt;/Grid&gt;
                &lt;Checkbox
                  label=&quot;Use same address&quot;
                  onClick={() =&gt; this.toggleAddress()}
                /&gt;
              &lt;/Form&gt;
            )}
          &lt;/Formik&gt;
        &lt;/&gt;
      );
    }
  }
  </code></pre>
  <p>So I've tried more ways to solve it but without success. Now in the code it is:</p>
  <pre><code> {this.state.setAddress
       ? values.address.setFieldValue(values.mainAddress)
       : console.log('nope')}
  </code></pre>
  <p>Other one was: <code>(this.state.setAddress ? values.address = values.mainAddress)</code></p>
  <p>None of them work. Is it possible to get the value from <code>values.mainAddress</code> and copy it to <code>values.address</code>? Or to its input?</p>