React selecting option with object as attribute value

前端 未结 4 851
时光说笑
时光说笑 2020-12-11 00:29

I have an issue with react when I want to change the selected option. The problem is that the value is an object and I can\'t pass it in option value attribut.

See t

4条回答
  •  误落风尘
    2020-12-11 01:01

    Try this following code,

    import React from 'react';
    
    class LocationDemo extends React.Component {
    
        constructor(props) {
            super(props);
            this.state = {
                searchLoc: undefined,
                selectedLoc: "",
                locs:[
                    {"name" : "Kerala","districts":["Ernakulam", "Trivandrum"]},
                    {"name" :"Tamil Nadu","districts" :["Palani","Tiruchi"]}
                ],
            };
            this.handleChangeLocation = this.handleChangeLocation.bind(this);
        }
    
    
        handleChangeLocation = (event) => {
            this.setState({ selectedLoc: event, searchLoc: event.target.value }
                , () => console.log("searchLoc", this.state.searchLoc));
        }
    
        render() {
            return (
                
    
            );
        }
    }
    
    export default LocationDemo;
    

提交回复
热议问题