Попробуйте метод async / await, вы получите сообщение об ошибке, потому что данные не загружены, а функция рендеринга пытается загрузить данные.
async componentDidMount() {
await fetch(`http://www.example.com/React/data.php`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
}).then((response) => response.json()).then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson,
id: responseJson[0].id
});
}).catch((error) => {
console.error(error);
});
}
Или другой подход заключается в добавлении предзагрузчика загрузкиили прядильщик, как это.
Сначала импортируйте пакет.
import { ActivityIndicator } from 'react-native';
Затем измените метод рендеринга
render() {
if (isLoading) {
return ( <
View style = {
[styles.container, styles.horizontal]
} >
<
ActivityIndicator size = "large"
color = "#0000ff" / >
<
/View>
);
}
return (
// Your render stuffs
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center'
},
horizontal: {
flexDirection: 'row',
justifyContent: 'space-around',
padding: 10
}
})
Если возникнет какая-либо проблема, сообщите мне