Andre Roque
Andre Roque

Reputation: 543

ReactJS state depending on another state

I'm trying to do a component that have an object state, where one of the properties is basically another state, something like this:

this.state = {
stateNameList : [],
fieldProperties = 
        [
            { type: FieldType.ComboBox, label: "State", fieldId: "state", source:this.state.stateNameList }
}

Is there a way to do it? Because that stateNameList will be populated on the componentWillMount function, by calling an API service.

Upvotes: 0

Views: 111

Answers (1)

luiscrjr
luiscrjr

Reputation: 7258

If you want to set the state based on the state, you have to create this.state first, and then add new keys based on the already created object. Something like that:

this.state = {
    stateNameList : [],
    fieldProperties: []
}

this.state.fieldProperties.push(
    { 
        type: FieldType.ComboBox, 
        label: "State", 
        fieldId: "state", 
        source: this.state.stateNameList //now it knows what this.state is
    }
)

Besides this, you were setting fieldProperties value with = operator, and not :. And you were not closing the fieldProperties brackets.

Upvotes: 1

Related Questions