First, thanks for reading me. I'm working on react-native using Expo. I set up functions in my app using componentWillMount and componentDidMount. It's working, ok but I have difficulties understanding why my component renders 4 times...
I get this result (with my console.log) :
Results retrieveDeviceManufacturer() : 42 ===>retrieveProfileUserId 42 Results retrieveDeviceUID() : ... Results retrieveDeviceOSVersion() : 10 Results retrieveDeviceManufacturer() : Google Connection type wifi Is connected? true
But 4 times in a row each times I execute the component. I'm of course new to react-native and need help to fully understand this.
Thanks a lot if you can help. Here is my full page of code :
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { isFirstConnection: true };
status: "0";
deviceOSVersion: null;
deviceManufacturer: null;
deviceUID: null;
profileUserId: null;
}
performTimeConsumingTask = async () => {
return new Promise(resolve =>
setTimeout(() => {
resolve("result");
}, 1000)
);
};
componentWillMount = async () => {
this.setState({ deviceOSVersion: await retrieveDeviceOSVersion() });
this.setState({ deviceManufacturer: await retrieveDeviceManufacturer() });
this.setState({ deviceUID: await retrieveDeviceUID() });
this.setState({ profileUserId: await retrieveProfileUserId() });
};
async componentDidMount() {
this.setState({ status: "6" });
// Preload data from an external API
// Preload data using AsyncStorage
const data = await this.performTimeConsumingTask();
if (data !== null) {
this.setState({ isFirstConnection: false });
}
Font.loadAsync({
Roboto: require("./assets/fonts/Roboto-Black.ttf")
});
}
render() {
if (this.state.status == "6") {
console.log(
"Results retrieveDeviceManufacturer() : ",
this.state.profileUserId
);
if (this.state.profileUserId !== null && this.state.profileUserId > 0) {
// OK
} else {
// Need connection
}
console.log("===>retrieveProfileUserId", this.state.profileUserId);
// Device UUID
console.log("Results retrieveDeviceUID() : ", this.state.deviceUID);
if (this.state.deviceUID !== null) {
// OK
} else {
// TODO : next step...
storeDeviceUID(getDeviceUID());
}
// Detect Manufacturer : iOS, Android, ..
if (this.state.deviceManufacturer !== null) {
// OK
} else {
storeDeviceManufacturer(getDeviceManufacturer());
}
// Get system version
if (this.state.deviceOSVersion !== null) {
// OK
} else {
storeDeviceOSVersion(getDeviceOSVersion());
}
console.log(
"Results retrieveDeviceOSVersion() : ",
this.state.deviceOSVersion
);
console.log(
"Results retrieveDeviceManufacturer() : ",
this.state.deviceManufacturer
);
NetInfo.fetch().then(state => {
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
});
if (this.state.isFirstConnection) {
return <SplashScreen />;
}
return <Navigation />;
} else {
console.log("STATUS INITIALISATION");
this.setState({ status: "1" });
return null;
}
}
}