首先,感谢您的阅读。我正在使用Expo开发本机。我使用componentWillMount和componentDidmount在我的应用中设置功能。 可以,但是我很难理解为什么我的组件渲染了4次...
我得到这个结果(通过console.log):
结果retrieveDeviceManufacturer():42 ===> retrieveProfileUserId 42结果retrieveDeviceUID():...结果retrieveDeviceOSVersion():10个结果 resolveDeviceManufacturer():Google连接类型wifi是 连接的?是
但是我每次执行组件都连续4次。 我当然是本机反应的新手,需要帮助才能完全理解这一点。
非常感谢您的帮助。这是我的整页代码:
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;
}
}
}