I have an object which includes several fields, most values are strings (constants) but I also want to use a variable (populated using some state)
const {order} = this.state;
myObject={{
fieldA: 2,
fieldB: ${order.value}
}}
I tried different variation of the above (with/without single quotes, etc.) but I am never able to set the value I need.
You are adding 2 brackets for an object which is not valid and no need of template literal.
const { order } = this.state;
const myObject = {
fieldA: 2,
fieldB: order.value,
};
If this.state looks like this:
this.state = {order: ""}
Declaring
const {order} = this.state
would just create a constant called order that is holding the value of this.state.order at the time order was declared. It will not be updated when this.state.order is updated.
If you want to set myObject.fieldB to hold a reference to the value of this.state.order, that is, the state value of order, do this:
const myObject = {
fieldA: 2,
fieldB: this.state.order,
}
You have used two braces ('{}') which are not needed. The code should be like below
const myobject = {fieldA: 2, fieldB: order.value};