我从服务器获取一些数据以将其加载到我的应用程序,我的应用程序无法加载,直到应用程序加载所有数据,它在调试模式下完美运行,但是当我必须在发布模式下测试它,我必须重新加载才能使它工作,这不是主意。这是我的代码:
import React, { Component } from 'react';
import { Alert, NetInfo, View, Text, AsyncStorage, TouchableWithoutFeedback, Linking, Platform } from 'react-native';
import { Actions } from 'react-native-router-flux';
import Router from './Router';
import OnBoarding from './components/OnBoarding';
import Loading from './components/Loading';
import OneSignal from 'react-native-onesignal';
import axios from 'axios';
var DeviceInfo = require('react-native-device-info');
class App extends Component {
constructor(props) {
super(props);
this.state = {
usage: '',
categories_hum: [],
videoshum: [],
categories_nov: [],
novedades: [],
tiendas: [],
promociones: [],
listadoCodigos: [],
token: "",
listadoCodigosRecibido: true,
terminos: '',
notFirst: false
};
this.changeUsage = this.changeUsage.bind(this);
}
onIds(device) {
AsyncStorage.setItem('playerId', device.userId);
}
onOpened(openResult) {
if(openResult.notification.payload.additionalData != undefined) {
var opc = openResult.notification.payload.additionalData.opc;
if(opc == 2) {
opc = "2";
AsyncStorage.setItem('opcion', opc);
} else if(opc == 1) {
opc = "1";
AsyncStorage.setItem('opcion', opc);
}
}
}
componentWillMount() {
OneSignal.addEventListener('ids', this.onIds);
OneSignal.addEventListener('opened', this.onOpened);
}
componentDidMount() {
NetInfo.isConnected.fetch().then(isConnected => {
if(!isConnected) {
Alert.alert (
'No hay conexion',
'No hay conexion a internet, debe poseer una conexion WiFi o celular para usar FerretotalApp'
);
}
});
fetch('https://ferrretotalcom.kinsta.com/wp-json/wp/v2/categoria-hum?per_page=100')
.then(response => response.json())
.then(
response => this.setState({ categories_hum: response})
);
fetch('https://ferrretotalcom.kinsta.com/wp-json/wp/v2/videoshum?per_page=100')
.then(response => response.json())
.then(
response => this.setState({ videoshum: response })
);
fetch('https://ferrretotalcom.kinsta.com/wp-json/wp/v2/tienda_acf?per_page=100')
.then(response => response.json())
.then(
response => this.setState({ tiendas: response })
);
fetch('https://ferrretotalcom.kinsta.com/wp-json/wp/v2/categorianovedades?per_page=100')
.then(response => response.json())
.then(
response => this.setState({ categories_nov: response })
);
fetch('https://ferrretotalcom.kinsta.com/wp-json/wp/v2/ferretotalnovedades?per_page=100')
.then(response => response.json())
.then(
response => this.setState({ novedades: response })
);
fetch('https://ferrretotalcom.kinsta.com/wp-json/wp/v2/promociones_activas?hola=1')
.then(response => response.json())
.then(
response => this.setState({ promociones: response })
);
fetch('https://ferrretotalcom.kinsta.com/wp-json/wp/v2/listado_codigos_usuario?deviceID="'+DeviceInfo.getUniqueID()+'"')
.then(response => response.json())
.then(
response => this.setState({ listadoCodigos: response, listadoCodigosRecibido: true})
);
fetch('https://ferrretotalcom.kinsta.com/wp-json/wp/v2/terminos_condiciones?hola=1')
.then(response => response.json())
.then(
response => this.setState({ terminos: response})
);
AsyncStorage.getItem('usage').then((result) => {
if(!result){
this.setState({usage: "firstTime"});
}else{
this.setState({usage: result});
}
//al colocar esto hara q salga siempre el onboarding
//AsyncStorage.removeItem("usage");
});
AsyncStorage.getItem('notFirst').then((result) => {
if(!result){
this.setState({notFirst: false});
} else {
this.setState({notFirst: false})
}
});
AsyncStorage.getItem('token').then((value) => {
if(!value){
var DeviceID = DeviceInfo.getUniqueID();
fetch('https://ferrretotalcom.kinsta.com/wp-json/wp/v2/recibir_token?deviceID="'+DeviceID+'"')
.then((response) => response.json())
.then((response) => {
if(response[0]){
AsyncStorage.setItem('token',response[0].access_token);
this.setState({token: response[0].access_token});
}
})
} else {
this.setState({token: value})}
});
AsyncStorage.setItem('newCode',"false");
}
componentWillUnmount() {
OneSignal.removeEventListener('ids', this.onIds);
OneSignal.removeEventListener('opened', this.onOpened);
}
changeUsage(e) {
this.setState({usage: "notFirst"});
this.setState({notFirst: true});
}
render(){
/*alert(this.state.categories_hum.length + " " + this.state.videoshum.length + " " + this.state.promociones.length
+ " " + this.state.novedades.length + " " + this.state.categories_nov.length + " " + this.state.tiendas.length + " " + this.state.listadoCodigosRecibido + " " + this.state.terminos.length) */
if(this.state.categories_hum.length && this.state.videoshum.length && this.state.promociones.length
&& this.state.novedades.length && this.state.categories_nov.length && this.state.tiendas.length && this.state.listadoCodigosRecibido && this.state.terminos.length) {
if(this.state.usage.length && this.state.usage == "firstTime"){
//al colocar esto solo saldra el onboarding la primera vez
AsyncStorage.setItem('usage', "notFirst");
AsyncStorage.setItem('notFirst', true)
//al colocar esto, guardara la fecha de instalacion de la aplicacion (realmente la primera vez que se mete)
AsyncStorage.getItem('installed').then((result) => {
if(!result) {
var date = new Date();
date = date.toString();
AsyncStorage.setItem('installed', date);
}
});
return (
<OnBoarding changeUsage={this.changeUsage} terminos={this.state.terminos}/>
);
} else if(this.state.usage == "notFirst" && this.state.notFirst == false) {
return(
<OnBoarding changeUsage={this.changeUsage} terminos={this.state.terminos} notFirst={true}/>
);
} else if(this.state.usage == "notFirst" && this.state.notFirst) {
return (
<View style={{flex:1}}>
<Router
categories_hum={this.state.categories_hum}
videoshum={this.state.videoshum}
categories_nov={this.state.categories_nov}
novedades={this.state.novedades}
tiendas={this.state.tiendas}
listadoCodigos={this.state.listadoCodigos}
promociones={this.state.promociones}
token={this.state.token}
terminos={this.state.terminos}
/>
</View>
);
}
} else{
return (
<Loading/>
)
}
}
}
export default App;
正如您所看到的,我在&#34; ComponentDidMount&#34;中提取了我需要的所有数据。方法,然后我将JSON数据存储在多个状态并将其传递到我需要的屏幕,事情是在发布模式下,状态在它之后没有任何东西&#34;加载&#34;它只在你第一次打开应用程序时发生,并且只在发布模式下,我检查来自fetch的响应,它没关系,它带来了数据。我已经尝试了很多东西,但我仍然无法弄清楚它是什么,因为它在调试模式下运行良好。如果您有任何想法,请告诉我。
答案 0 :(得分:0)
从描述中看,任务似乎与预期的行为不同步。 setState默认情况下是异步的,但是根据我的经验,我知道有些情况下必须使用“ Async”和“ await”使其显式异步。 我不确定,但是在每个setState调用方法中,也可能会出现以下情况:
async changeUsage(e) {
await this.setState({usage: "notFirst"});
await this.setState({notFirst: true});
}