#How to handle mapped inputs formdata?

4 messages · Page 1 of 1 (latest)

rich pike
#

With single input fields inside a Form tag data gets into the action via name="xyz", but how does it work for a list of inputs? 🤔

#
{initialData.value?.selectors.map((option, index) => (
  <input type="checkbox" name={`option-${index}`} value={option.id} />
))}

There must be a better way?

charred wolf
#

currently it uses dot notation to create objects and [] for an array. note that currently it doesn't support an array of objects. So you could do:

{initialData.value?.selectors.map((option, index) => (
  <input type="checkbox" name={`options.${index}`} value={option.id} />
))}

and that would result as:

{
  options: {
    1: true,
    2: true,
    3: false
  }
}```
#

iirc