Tuesday, May 28, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  90] [ 5]  / answers: 1 / hits: 9877  / 4 Years ago, fri, may 8, 2020, 12:00:00

I am new to React Native and I was trying to display an ActivityIndicator when my fetch function is loading. I was looking how to implement it and I think I need to use Render(){} function but it showing me and error of semicolon



this is my code so far:





import React, {coponent} from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';


export default function App() {


this.state = {
isLoading: true,
}



const [nombre,setNombre]= React.useState('');

const fetchDatos = async () => {
return fetch('http://localhost:8000/api/consulta', {method: 'POST', headers: new Headers({'Accept': 'application/json',
'Content-Type': 'application/json',

}), body: JSON.stringify({
codigoParticipante: '1520',
contrato: '135927',
})}).then(response => {
return response.json();
})
.then(responseJson => {

if(responseJson.Participante['@attributes'].Cod == 1){
switch (responseJson.Participante.Status.Codigos['@attributes'].Codigo) {
case '400':
alert(responseJson.Participante.Status.Codigos['@attributes'].Error);
break;
case '300':
alert(responseJson.Participante.Status.Codigos['@attributes'].Error);
break;
default:
console.log('Error interno');
break;
}
}else{
this.setState({ isLoading: false })
setNombre(responseJson.Participante.InfoParticipante['@attributes'].Nombre);
}}).catch(function() {
alert(No es posible conectar con el servidor.);
});
}
render(){
const { isLoading} = this.state;

return (
<View>
<Button
title='press me'
onPress={fetchDatos}
/>
<Text>{nombre}</Text>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});





Error



Do someone know what I am doing wrong? I will appreciate it a lot!


More From » reactjs

 Answers
2

You mixed up class base component and functional component.



This is functional component:



import React from react;
import { StyleSheet, Text, View, Button } from react-native;

export default function App() {
const [loading, setLoading] = React.useState(true);
const [nombre, setNombre] = React.useState();

const fetchDatos = async () => {
return fetch(http://localhost:8000/api/consulta, {
method: POST,
headers: new Headers({ Accept: application/json, Content-Type: application/json }),
body: JSON.stringify({
codigoParticipante: 1520,
contrato: 135927
})
})
.then(response => {
return response.json();
})
.then(responseJson => {
if (responseJson.Participante[@attributes].Cod == 1) {
switch (responseJson.Participante.Status.Codigos[@attributes].Codigo) {
case 400:
alert(responseJson.Participante.Status.Codigos[@attributes].Error);
break;
case 300:
alert(responseJson.Participante.Status.Codigos[@attributes].Error);
break;
default:
console.log(Error interno);
break;
}
} else {
this.setState({ isLoading: false });
setNombre(responseJson.Participante.InfoParticipante[@attributes].Nombre);
}
})
.catch(function() {
alert(No es posible conectar con el servidor.);
});
};

return (
<View>
<Button title=press me onPress={fetchDatos} />
<Text>{nombre}</Text>
</View>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: #fff,
alignItems: center,
justifyContent: center
}
});


In functional component there is no render method and you have to define state with useState so this.state not work.



and also coponent is not correct.


[#3875] Wednesday, May 6, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aileent

Total Points: 556
Total Questions: 107
Total Answers: 101

Location: Croatia
Member since Fri, Sep 11, 2020
4 Years ago
;