0

I have an array which you can see below. How can I insert data from a Selectfield into prints.printMethod?

selectedOrder: {
  category: {},
  product: {},
  brand: {},
  prints: {
    printMethod: {},
    height: '',
    width: '',
    colors: ''
  }
}

My code below is the onChange function. I am not sure if I am missing something. I hope someone can help me.

onBrandingsChange = (event, index, value) => {
    var newSelect = this.state.selectedOrder;
    newSelect.prints.push(value);
    this.setState({selectedOrder: newSelect});
  }

 <SelectField value={this.state.selectedOrder.prints.printMethod} onChange={this.onBrandingsChange}>
    {_.map(this.state.brandings, (b) => {
      return <MenuItem value={b.name} primaryText={b.name} key={b.id}/>;
    })}
</SelectField>

1 Answer 1

1

prints is an Object not an array type so push can't be used here.

You can assign the value to the key directly if you wish it to be single depth.

Something like:

onBrandingsChange = (event, index, value) => {
    var newSelect = this.state.selectedOrder;
    // set it
    newSelect.prints.printMethod = value
    // or use assign
    newSelect.prints = Object.assign(newSelect.prints, { printMethod: value });

    this.setState({selectedOrder: newSelect});
}

Of if you wish this to be an array you need to change the types of your selectedOrder state.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.