question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Field with Array of objects (Unified Mode)

See original GitHub issue

I’m having a field tags which is supposed to be an array of objects. Using react-select with multi I’m able to add tag objects (with an id and a name) to the array (Note that that id and name are not editable, you can just add/remove objects).

Starting with an empty array this works flawlessly, but when initializing the form with an object which has already some tags the field’s value always returns the inital array. I did a little bit of debugging and discovered that when I start with an emtpy array field.hasNestedFields is false while when starting with a pre-populated one it changes to true.

export const fields = [
  //... some other fields...
  {
    name: 'tags',
    label: 'Tags',
    value: []
  }
]

// .....
const form = new EditForm({fields}, {plugins});

//... load some item
form.update(item);

// the field component
class TagInput extends React.Component {
  render() {
    return (		
                <Select.AsyncCreatable
		    {...field.bind()}
		    multi
		    valueKey="id"
		    labelKey="name"
		    resetValue={[]}
		    searchable={true}
		    clearable={true}
		    loadOptions={this.getTags}
		/>
    )
  }
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
bvallantcommented, May 12, 2017

Thanks for your help again, after some poking around I came up with this onChange handler:

handleChange(e) {
  const {field} = this.props
  // clear all "old" values 
  // field.clear() / .reset() didnt work for me 
  // so clearing the mobx array myself
  field.fields.clear()

   // add the currently selected values
   e.map(v => field.add(v));
}

This basically works now for me, thanks for poinint me to the onChange thing.

0reactions
foxhound87commented, Apr 17, 2018

I’ve updated the package introducing the struct prop.

Take a look here: https://github.com/foxhound87/mobx-react-form/blob/master/tests/data/forms/fixes/form.q1.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

Array Object - A.I. Solutions
You can assign Array objects using the = operator. Note: In millisecond mode, if the number of elements is not specified, the Array...
Read more >
Work with arrays | BigQuery - Google Cloud
You can construct arrays of simple data types, such as INT64 , and complex data types, such as STRUCT s. The current exception...
Read more >
Create a custom action to generate an array of objects from a ...
Generate an array of objects from a list of User records. Learn how to use a Script step to iterate through a list...
Read more >
Arrays - C# Programming Guide | Microsoft Learn
You declare an array by specifying the type of its elements. If you want the array to store elements of any type, you...
Read more >
Working with Arrays | Unreal Engine 4.27 Documentation
Creating an Array variable can be done with the following steps. Inside your Blueprint click the Add Variable button, give the variable a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found