I have a JSON with all states and cities. I want to make when selecting the state it shows me only the cities regarding the state already selected select.
My code prints the cities. with console.war
, but the return is undefined
.
The following information.
JSON
{
"estados": [
{
"sigla": "AC",
"nome": "Acre",
"cidades": [
"Acrelândia",
"Assis Brasil",
"Brasiléia",
"Bujari",
]
},
{
"sigla": "AL",
"nome": "Alagoas",
"cidades": [
"Água Branca",
"Anadia",
"Arapiraca",
"Atalaia",
]
}
]
}
Code:
_getItem = (val) => {
JSONCidades.estados.map(element => {
if (val == element.sigla) {
console.warn(element.cidades)
return element.cidades
}
})
}
_onChageValue = (value) => {
this.setState({ uf: value })
}
_onChageValue2 = (value) => {
console.warn(value)
}
<Picker
selectedValue={this.state.uf}
style={{ height: 50, width: 120 }}
onValueChange={this._onChageValue.bind(this)}
>
{
JSONCidades.estados.map((element, index) => {
return <Picker.Item label={element.sigla} value={element.sigla} />
})
}
<Picker
selectedValue={this.state.cidade}
style={{ height: 50, width: 120 }}
onValueChange={this._onChageValue2.bind(this)}
>
{
this._getItem(this.state.uf.toString())
}
</Picker>