#thedivisor#In the previous example, the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">position</code> object is always created fresh from the current cursor position. But often, you will want to include <em>existing</em> data as a part of the new object you’re creating. For example, you may want to update <em>only one</em> field in a form, but keep the previous values for all other fields.#thedivisor#These input fields don’t work because the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">onChange</code> handlers mutate the state:#thedivisor#For example, this line mutates the state from a past render:#thedivisor#The reliable way to get the behavior you’re looking for is to create a new object and pass it to <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">setPerson</code>. But here, you want to also <strong class="font-bold">copy the existing data into it</strong> because only one of the fields has changed:#thedivisor#You can use the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">...</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#spread_in_object_literals" target="_blank" rel="nofollow noopener noreferrer" class="inline text-link dark:text-link-dark border-b border-link border-opacity-0 hover:border-opacity-100 duration-100 ease-in transition leading-normal">object spread</a> syntax so that you don’t need to copy every property separately.#thedivisor#Now the form works!#thedivisor#Notice how you didn’t declare a separate state variable for each input field. For large forms, keeping all data grouped in an object is very convenient—as long as you update it correctly!#thedivisor#Note that the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">...</code> spread syntax is “shallow”—it only copies things one level deep. This makes it fast, but it also means that if you want to update a nested property, you’ll have to use it more than once.#thedivisor#You can also use the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">[</code> and <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">]</code> braces inside your object definition to specify a property with a dynamic name. Here is the same example, but with a single event handler instead of three different ones:#thedivisor#Here, <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">e.target.name</code> refers to the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">name</code> property given to the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><input></code> DOM element.
#thedivisor#In the previous example, the position object is always created new from the current cursor position. But many times, you will want to include existing data as part of the new object you are creating. For example, you may want to update only one field in a form, but keep the previous values for all other fields.#thedivisor#These input fields do not work because the onChange handlers change the state:#thedivisor#For example, this line changes the state from a previous render:#thedivisor#The best way to get the behavior you want is to create a new object and pass it to setPerson. But here, you also want to copy the existing data into it because only one of the fields has changed:#thedivisor#You can use the ... object spread syntax so you do not need to copy every property separately.#thedivisor#Now the form works!#thedivisor#Notice that you did not declare a separate state variable for each input field. For large forms, keeping all data grouped in an object is very useful—as long as you update it correctly!#thedivisor#Note that the ... spread syntax is “shallow”—it only copies items one level deep. This makes it fast, but it also means that if you want to update a nested property, you will have to use it more than once.#thedivisor#You can also use the [ and ] braces in your object definition to specify a property with a dynamic name. Here is the same example, but with one event handler instead of three different ones:#thedivisor#Here, e.target.name refers to the name property given to the <input> DOM element.