Javascript 警告:Can';t在未安装的组件上执行React状态更新。如何使用它 如何使用它?
我的代码在登录时出现问题,当我更改屏幕并完成身份验证时。我不知道解决方案是什么,但我认为错误的是,我不知道如何使用did挂载:D我寻找did挂载,但我不知道如何使用它,更重要的是,我必须找出它是如何工作的 问题 APP.jsJavascript 警告:Can';t在未安装的组件上执行React状态更新。如何使用它 如何使用它?,javascript,react-native,react-router,Javascript,React Native,React Router,我的代码在登录时出现问题,当我更改屏幕并完成身份验证时。我不知道解决方案是什么,但我认为错误的是,我不知道如何使用did挂载:D我寻找did挂载,但我不知道如何使用它,更重要的是,我必须找出它是如何工作的 问题 APP.js import React,{useState,useffect}来自“React”; 从“@react-navigation/native”导入{NavigationContainer}”; 从“/Routes/MainStack.Routes”导入MainStack; 从
import React,{useState,useffect}来自“React”;
从“@react-navigation/native”导入{NavigationContainer}”;
从“/Routes/MainStack.Routes”导入MainStack;
从“/Routes/HomeStack.Routes”导入HomeStack;
从“@react native firebase/auth”导入身份验证;
从“/Components/Load”导入负载;
从“@react native async storage/async storage”导入异步存储;
导出默认函数App(){
const[initializing,setInitializing]=useState(true);
const[user,setUser]=useState();
异步函数onAuthStateChanged(userLoged){
setUser(userLoged);
如果(用户登录){
等待AsyncStorage.setItem(“lolguide@user,JSON.stringify(userLoged));
}
if(初始化)setInitializing(false);
}
useffect(()=>{
返回auth().onAuthStateChanged(onAuthStateChanged);
}, []);
如果(初始化){
返回;
}
如果(!用户){
返回(
);
}否则{
返回(
);
}
}
HomeStack.js
从“React”导入React;
从“@react navigation/stack”导入{createStackNavigator};
从“./组件/负载”导入负载;
从“./Routes/MainStack.Routes”导入MainStack屏幕;
从“./Pages/Home/index”导入主页;
从“./页面/配置文件”导入配置文件;
从“./Pages/Champ/index”导入Champ;
const-MainStack=createStackNavigator();
const HomeStackScreens=()=>(
);
导出默认屏幕;
MainStack.js
从“React”导入React;
从“@react navigation/stack”导入{createStackNavigator};
从“./Pages/Login/index”导入登录名;
从“./页面/注册/索引”导入注册;
从“./组件/负载”导入负载;
从“./Routes/HomeStack.Routes”导入主页;
const-MainStack=createStackNavigator();
const HomeStack=()=>(
);
出口违约本币;
家
import React,{useState,useffect}来自“React”;
从“/styles”导入{Container};
从“../../Components/Header”导入标题;
从“../../Components/Load”导入负载;
从“../../Components/ChampCard”导入ChampCard;
从“react native”导入{FlatList、StatusBar、TouchableOpacity};
从'@react-navigation/native'导入{useNavigation};
常量Home=()=>{
const[loading,setLoading]=useState(true);
const[champs,setChamps]=useState([]);
const navigation=useNavigation();
功能导航器(电视台、道具){
navigation.navigate(tela,{item:JSON.stringify(props)});
}
异步函数getData(){
待命(
"https://ddragon.leagueoflegends.com/cdn/11.10.1/data/pt_BR/champion.json"
)
.然后((res)=>res.json())
.then((json)=>setChamps(Object.values(json.data));
设置加载(假);
}
useffect(()=>{
getData();
}, []);
如果(装载){
返回;
}
返回(
{champs.length>0(
item.key}
renderItem={(项目)=>(
纳维加(“冠军”,项目)}>
)}
contentContainerStyle={{paddingBottom:65}
showsVerticalScrollIndicator={false}
/>
) : (
)}
);
};
导出默认主页;
Login.js
import React,{useState}来自“React”;
从“react native”导入{Image,Dimensions,PixelRatio,StyleSheet};
从“世博会状态栏”导入{StatusBar};
从“expo linear gradient”导入{LinearGradient};
从“@react-navigation/native”导入{useNavigation}”;
从“@react native async storage/async storage”导入异步存储;
进口{
集装箱,
标志
文本输入,
文本,
按钮
纽特纳里亚,
图像盒,
绝对视野,
}来自“/样式”;
从“../../Components/Load”导入负载;
从“@react native firebase/auth”导入身份验证;
常量宽度百分比TODP=(宽度百分比)=>{
const screenWidth=维度.get(“窗口”).width;
返回PixelRatio.roundToNearestPixel(
(屏幕宽度*parseFloat(宽度百分比))/100
);
};
常量高度百分比TODP=(高度百分比)=>{
const screenHeight=尺寸。获取(“窗口”)。高度;
返回PixelRatio.roundToNearestPixel(
(屏幕高度*parseFloat(高度百分比))/100
);
};
const styles=StyleSheet.create({
线性梯度:{
高度:高度百分比TODP(“120%”),
弹性:1,
},
});
常量登录=()=>{
const[email,setEmail]=useState(“”);
const[password,setPassword]=useState(“”);
const[isLoading,setIsLoading]=useState(false);
const navigation=useNavigation();
常量handleSend=()=>{
如果(电子邮件和密码){
设置加载(真);
auth()
.使用电子邮件和密码登录(电子邮件、密码)
.然后(()=>{
导航。导航(“主屏幕”);
设置加载(假);
})
.catch((错误)=>{
if(错误代码){
控制台错误(error);
}
});
设置加载(假);
}否则{
警报(“预检系统!”);
}
};
const handleRegisterNavigate=()=>{
导航。导航(“注册”);
};
如果(孤岛加载){
返回;
}
返回(
最佳课程安排
setEmail(t)}
/>
设置密码(t)}
/>
诱捕者
登记员
);
};
出口d
ERROR Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
in Home (at SceneView.tsx:122)
(...)
in StackNavigator (at MainStack.routes.js:11)
in HomeStack (at App.js:32)
import React, { useState, useEffect } from "react";
import { Container } from "./styles";
import Header from "../../Components/Header";
import Load from "../../Components/Load";
import ChampCard from "../../Components/ChampCard";
import { FlatList, StatusBar, TouchableOpacity } from "react-native";
import { useNavigation } from '@react-navigation/native';
const Home = () => {
const [loading, setLoading] = useState(true);
const [champs, setChamps] = useState([]);
const navigation = useNavigation();
function navegar(tela, props) {
navigation.navigate(tela, {item: JSON.stringify(props)});
}
async function getData() {
await fetch(
"https://ddragon.leagueoflegends.com/cdn/11.10.1/data/pt_BR/champion.json"
)
.then((res) => res.json())
.then((json) => setChamps(Object.values(json.data)));
setLoading(false);
}
useEffect(() => {
getData();
}, []);
if (loading) {
return <Load />;
}
return (
<>
<Container>
<Header type="NormalHeader"/>
{champs.length > 0 ? (
<FlatList
data={champs}
keyExtractor={(item) => item.key}
renderItem={(item) => (
<TouchableOpacity onPress={() => navegar("Champ", item)}>
<ChampCard item={item}/>
</TouchableOpacity>
)}
contentContainerStyle={{ paddingBottom: 65 }}
showsVerticalScrollIndicator={false}
/>
) : (
<Load />
)}
</Container>
<StatusBar backgroundColor="#000"/>
</>
);
};
export default Home;
import React, { useState } from "react";
import { Image, Dimensions, PixelRatio, StyleSheet } from "react-native";
import { StatusBar } from "expo-status-bar";
import { LinearGradient } from "expo-linear-gradient";
import { useNavigation } from "@react-navigation/native";
import AsyncStorage from '@react-native-async-storage/async-storage';
import {
Container,
Logo,
TextInput,
Text,
Button,
ButtonArea,
ImageBox,
ViewAbsolute,
} from "./styles";
import Load from "../../Components/Load";
import auth from "@react-native-firebase/auth";
const widthPercentageToDP = (widthPercent) => {
const screenWidth = Dimensions.get("window").width;
return PixelRatio.roundToNearestPixel(
(screenWidth * parseFloat(widthPercent)) / 100
);
};
const heightPercentageToDP = (heightPercent) => {
const screenHeight = Dimensions.get("window").height;
return PixelRatio.roundToNearestPixel(
(screenHeight * parseFloat(heightPercent)) / 100
);
};
const styles = StyleSheet.create({
linearGradient: {
height: heightPercentageToDP("120%"),
flex: 1,
},
});
const Login = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [isLoading, setIsLoading] = useState(false);
const navigation = useNavigation();
const handleSend = () => {
if (email && password) {
setIsLoading(true);
auth()
.signInWithEmailAndPassword(email, password)
.then( () => {
navigation.navigate("HomeScreen");
setIsLoading(false);
})
.catch((error) => {
if (error.code) {
console.error(error);
}
});
setIsLoading(false);
} else {
alert("Preencha os campos!");
}
};
const handleRegisterNavigate = () => {
navigation.navigate("SignUp");
};
if (isLoading) {
return <Load />;
}
return (
<ViewAbsolute>
<Container>
<LinearGradient
colors={["#C28F2C", "#000", "#004840"]}
style={styles.linearGradient}
start={{ x: 0, y: -0.2 }}
end={{ x: 0, y: 1.2 }}
>
<Logo>
<Image
source={require("../../Assets/lolGuideIcon.png")}
style={{ height: 75, width: 80 }}
resizeMode="stretch"
/>
</Logo>
<Text>O primeiro passo para se tornar um campeão é a iniciativa</Text>
<TextInput
placeholder="Email"
keyboardType="email-address"
margin_top="50px"
value={email}
onChangeText={(t) => setEmail(t)}
/>
<TextInput
placeholder="Senha"
margin_top="5px"
secureTextEntry
value={password}
onChangeText={(t) => setPassword(t)}
/>
<ButtonArea>
<Button width="80px" margin_top="20px" onPress={handleSend}>
<Text>Entrar</Text>
</Button>
<Button
width="110px"
margin_top="10px"
onPress={handleRegisterNavigate}
>
<Text>Registrar</Text>
</Button>
</ButtonArea>
<ImageBox>
<Image
source={require("../../Assets/Pyke-transparente.png")}
style={{
height: 230,
width: 260,
}}
/>
</ImageBox>
<StatusBar style="auto" backgroundColor="transparent" />
</LinearGradient>
</Container>
</ViewAbsolute>
);
};
export default Login;
import React, { useState, useEffect } from "react";
import { NavigationContainer } from "@react-navigation/native";
import MainStack from "./Routes/MainStack.routes";
import HomeStack from "./Routes/HomeStack.routes";
import auth from "@react-native-firebase/auth";
import Load from "./Components/Load";
import AsyncStorage from "@react-native-async-storage/async-storage";
export default function App() {
const [initializing, setInitializing] = useState(true);
const [user, setUser] = useState();
useEffect(() => {
auth().onAuthStateChanged((userLogged) => {
setUser(userLogged);
if (userLogged) {
AsyncStorage.setItem("lolguide@user", JSON.stringify(userLogged));
}
if (initializing) setInitializing(false);
});
}, [initializing]);
if (initializing) {
return <Load />;
}
if (!user) {
return (
<NavigationContainer>
<MainStack />
</NavigationContainer>
);
} else {
return (
<NavigationContainer>
<HomeStack />
</NavigationContainer>
);
}
}
import React, { useState } from "react";
import { Image, Dimensions, PixelRatio, StyleSheet } from "react-native";
import { StatusBar } from "expo-status-bar";
import { LinearGradient } from "expo-linear-gradient";
import { useNavigation } from "@react-navigation/native";
import AsyncStorage from '@react-native-async-storage/async-storage';
import {
Container,
Logo,
TextInput,
Text,
Button,
ButtonArea,
ImageBox,
ViewAbsolute,
} from "./styles";
import Load from "../../Components/Load";
import auth from "@react-native-firebase/auth";
const widthPercentageToDP = (widthPercent) => {
const screenWidth = Dimensions.get("window").width;
return PixelRatio.roundToNearestPixel(
(screenWidth * parseFloat(widthPercent)) / 100
);
};
const heightPercentageToDP = (heightPercent) => {
const screenHeight = Dimensions.get("window").height;
return PixelRatio.roundToNearestPixel(
(screenHeight * parseFloat(heightPercent)) / 100
);
};
const styles = StyleSheet.create({
linearGradient: {
height: heightPercentageToDP("120%"),
flex: 1,
},
});
const Login = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [isLoading, setIsLoading] = useState(false);
const navigation = useNavigation();
const handleSend = () => {
if (email && password) {
setIsLoading(true);
auth()
.signInWithEmailAndPassword(email, password)
//.then( () => {
// navigation.navigate("HomeScreen"); REMOVE THIS BLOCK
// setIsLoading(false);
//})
.catch((error) => {
if (error.code) {
console.error(error);
}
});
setIsLoading(false);
} else {
alert("Preencha os campos!");
}
};
const handleRegisterNavigate = () => {
navigation.navigate("SignUp");
};
if (isLoading) {
return <Load />;
}
return (
<ViewAbsolute>
<Container>
<LinearGradient
colors={["#C28F2C", "#000", "#004840"]}
style={styles.linearGradient}
start={{ x: 0, y: -0.2 }}
end={{ x: 0, y: 1.2 }}
>
<Logo>
<Image
source={require("../../Assets/lolGuideIcon.png")}
style={{ height: 75, width: 80 }}
resizeMode="stretch"
/>
</Logo>
<Text>O primeiro passo para se tornar um campeão é a iniciativa</Text>
<TextInput
placeholder="Email"
keyboardType="email-address"
margin_top="50px"
value={email}
onChangeText={(t) => setEmail(t)}
/>
<TextInput
placeholder="Senha"
margin_top="5px"
secureTextEntry
value={password}
onChangeText={(t) => setPassword(t)}
/>
<ButtonArea>
<Button width="80px" margin_top="20px" onPress={handleSend}>
<Text>Entrar</Text>
</Button>
<Button
width="110px"
margin_top="10px"
onPress={handleRegisterNavigate}
>
<Text>Registrar</Text>
</Button>
</ButtonArea>
<ImageBox>
<Image
source={require("../../Assets/Pyke-transparente.png")}
style={{
height: 230,
width: 260,
}}
/>
</ImageBox>
<StatusBar style="auto" backgroundColor="transparent" />
</LinearGradient>
</Container>
</ViewAbsolute>
);
};
export default Login;