[英]How to pass API (Axios) Response data across screens in React Native?
I stuck and do not know how to pass API Response data across screens.我卡住了,不知道如何跨屏幕传递 API 响应数据。
In screen #1 i do API call and get response data Now i need to use api-secret "key" from response data to Log out Screen.在屏幕 #1 中,我执行 API 调用并获取响应数据现在我需要使用响应数据中的 api-secret "key" 来注销屏幕。
I was reading about Redux, but got confused and could not fully get it.我正在阅读有关 Redux 的内容,但感到困惑并且无法完全理解它。
Looking for some alternatives how i can pass data.寻找一些替代方法,我可以如何传递数据。
My Code is我的代码是
class Login extends Component {
constructor(props) {
super(props);
this.state = {
user: {
login: "",
password: ""
},
activity: false
};
}
_login = async () => {
if (this.state.user.login !== "" && this.state.user.password !== "") {
console.log(this.state);
this.setState({ activity: true });
await Axios.post(LoginAPI, this.state, { headers: { appversion: 1.4 } })
.then(response => {
console.log(response);
const status = response.status;
if (status === 200) {
this.setState({ activity: false });
this.setState({ response });
this.props.navigation.navigate("CreateMove");
console.log(this.state);
}
})
.catch(error => {
console.log({ error });
this.setState({ activity: false });
Alert.alert("Error", error.response.data.error);
});
//Alert.alert("Easymove", "Login ");
//this.props.navigation.navigate("CreateMove");
} else {
Alert.alert("Support", "Email & Password field can not be empty");
}
};
}
And need To pass Response in State across screens并且需要跨屏幕传递状态响应
Youre already using react navigation so you can just pass the response as a param with the routename, or use setParam and getParam https://reactnavigation.org/docs/en/params.html您已经在使用反应导航,因此您可以将响应作为参数传递给路由名称,或者使用 setParam 和 getParam https://reactnavigation.org/docs/en/params.html
this.props.navigation.navigate("CreateMove", { response: response});
in your other screen you would get the response with在您的另一个屏幕中,您会得到响应
const response = this.props.navigation.getParam('response')
Since you don't want to use redux for your application use AsyncStorage in React Native.因为你不想在你的应用程序中使用 redux,所以在 React Native 中使用 AsyncStorage。
First, install npm i @react-native-community/async-storage
.首先,安装npm i @react-native-community/async-storage
。 Then change your _login
function as below.然后更改您的_login
功能,如下所示。 Don't use both Promise & Async/Await and use only one approach .不要同时使用 Promise 和 Async/Await 并且只使用一种方法。
import AsyncStorage from '@react-native-community/async-storage';
_login = async () => {
if (this.state.user.login !== "" && this.state.user.password !== "") {
this.setState({ activity: true });
try {
let response = await Axios.post(LoginAPI, this.state, {
headers: { appversion: 1.4 }
});
if (response.status == 200) {
this.setState({
activity: false,
response
});
await AsyncStorage.setItem('@Secret-Key', response);
this.props.navigation.navigate("CreateMove");
}
} catch (error) {
this.setState({ activity: false });
Alert.alert("Error", error.response.data.error);
}
} else {
Alert.alert("Support", "Email & Password field can not be empty");
}
};
Then get @Secret-Key
value from LogOut Screen.然后从注销屏幕获取@Secret-Key
值。
_retrieveData = async () => {
try {
const value = await AsyncStorage.getItem("@Secret-Key");
if (value !== null) {
// You can access your data
}
} catch (error) {
console.log(error);
}
};
Hope this will helps you.希望这会帮助你。 Feel free for doubts.如有疑问,请放心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.