<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Annuli</title>
    <description>The latest articles on Forem by Annuli (@annuli).</description>
    <link>https://forem.com/annuli</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F994306%2F053cae7e-bc38-4f47-b31e-465a0ce27e22.jpeg</url>
      <title>Forem: Annuli</title>
      <link>https://forem.com/annuli</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/annuli"/>
    <language>en</language>
    <item>
      <title>How do I create/save a dynamically created input box/label into a stateful array?</title>
      <dc:creator>Annuli</dc:creator>
      <pubDate>Wed, 21 Dec 2022 20:13:22 +0000</pubDate>
      <link>https://forem.com/annuli/how-do-i-createsave-a-dynamically-created-input-boxlabel-into-a-stateful-array-35p7</link>
      <guid>https://forem.com/annuli/how-do-i-createsave-a-dynamically-created-input-boxlabel-into-a-stateful-array-35p7</guid>
      <description>&lt;p&gt;Hello!&lt;br&gt;
I am trying to save a dynamically created label/input box into a stateful array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Background:&lt;/strong&gt; There is a checkbox component that passes all ticked check boxes to an array in the parent. The parent passes that state to a hidden component that ONLY opens when certain ticked check boxes are filtered through.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; I want to dynamically create an object using the string from the checkbox selections and the value from the input box and save to the state of the hidden component. If the user unchecks the checkbox, the those values would be removed from the array.&lt;/p&gt;

&lt;p&gt;Homework: I've looked at 4 different sources and these two&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://codingstatus.com/add-remove-multiple-input-fileds-dynamically-in-react-js/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodingstatus.com%2Fwp-content%2Fuploads%2F2022%2F01%2Freact-add-remove-multiple-input-fields-at-a-time.png" height="365" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://codingstatus.com/add-remove-multiple-input-fileds-dynamically-in-react-js/" rel="noopener noreferrer" class="c-link"&gt;
          Add/remove Multiple Input Fileds dynamically in React Js
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          You will learn to add and remove multiple input fields dynamically on a button click with react js. It is explained with complete code
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodingstatus.com%2Fwp-content%2Fuploads%2F2020%2F06%2Fcropped-codingstatus-icon-32x32.jpg" width="32" height="32"&gt;
        codingstatus.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/9IhsYu4eKJ8"&gt;
&lt;/iframe&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;are the closest approaches that might work for my situation. In most cases, I can create the input boxes for each string, however, I can't find a way to save the two pieces of information in the component's state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Current Results:&lt;/strong&gt; I have 1 input box for each label (the ticked checkbox), but only the input box values save to state. How could I save the label AND input box value to this component's state? Thank you!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // state
   const [transitCosts, setTransitCosts] = useState([{
      modalTransit: '',
      dailyModalCost:'',  
   }]);

   // Set the input box data to state
   const addTransitCosts =()=&amp;gt;{
      setTransitCosts([...transitCosts, {
         modalTransit: '',
         dailyModalCost:'' 
      }]) 
   };


    // For the input box value
   const handleDailyCosts =(index, e)=&amp;gt;{  // HOW TO SAVE 2 VALUES WITH 1 INDEX?

      const { name, value } = e.target;
      const list = [...transitCosts];
      list[index][name] = value;
      setTransitCosts(list);
   }

return (
   &amp;lt;div key={key}&amp;gt;                                      

      {checkedCategories.filter(category =&amp;gt; category !== 'Vehicle' &amp;amp;&amp;amp; category !== 'Walking').map((categoryData, index) =&amp;gt; {                            
      return(
         &amp;lt;div key={index}&amp;gt;
            &amp;lt;div className={`${styles.insetBox}`} onChange={addTransitCosts}&amp;gt;
               &amp;lt;label className={`${styles.containerLabel}`}  htmlFor='modalDailyCost'&amp;gt;{categoryData}&amp;lt;/label&amp;gt;
            &amp;lt;/div&amp;gt;                              

            {transitCosts.map((data, index) =&amp;gt; {                                                                                                            
             return(
                &amp;lt;div key={index} className={`${styles.dataInputBox}`}&amp;gt;
                   &amp;lt;div className={`${styles.insetBox}`}&amp;gt;   
                      &amp;lt;input type="text" 
                         onChange={(e)=&amp;gt;handleDailyCosts(index, e)}
                         name="dailyModalCost" 
                         value={dailyModalCost} 
                         id="dailyModalCost" 
                         placeholder="0.00" 
                       /&amp;gt;
                    &amp;lt;/div&amp;gt;
                 &amp;lt;/div&amp;gt;
               );
            })}
         &amp;lt;/div&amp;gt;
       );
    })}                                                                 
 &amp;lt;/div&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>devops</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
