2
I have a Json with all the states and cities. I want to do when selecting the state it shows me only the cities related to the state of the selected select.
My code prints the cities. with the console.war
, but the return is undefined
.
Follow the 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>
Friend your function is correct, it gives the warn with the cities and then printa Undefined, if you switch to the console.log() may further clarify the answer. But its function is ok. After it prints the values it always prints a Undefined.
– Maycon F. Castro