Linked Questions

Popular Questions

How to add key name and key value from Form?

Asked by At

I'm working on a React app and want the form I'm working on to be able to add key name and key value to sensorProperties: {} (sensorProperties is a part of sensor object). I have to allow user to give a name for both, key and value.

  this.state = {
        sensor: {
            name: '',
            type: '',
            position: '',
            sensorProperties: {}
        },
        show: false,
        key: '',
        value: ''
    };

I added two functions handleValueChange and handleKeyChange. When I invoke console.log(key, value), I see they have my input values, but how can I pass these values to my sensorProperties?

handleKeyChange = (event) => {
    const nKey = event.target.value
    this.setState({key: nKey})
}

handleValueChange = (event) => {
    const nValue = event.target.value
    this.setState({value: nValue})
}

 <FormGroup>
    <ControlLabel>Key</ControlLabel>
     <FormControl required type="text" value={key}  
     onChange={this.handleKeyChange}/>
 </FormGroup>

 <FormGroup>
     <ControlLabel>Value</ControlLabel>
     <FormControl required type="text" value={value} 
     onChange={this.handleValueChange}/>
 </FormGroup>

Related Questions