React native 如何在React Native中导出和导入用户Id
我需要从API中获取用户ID,然后将其附加到一个变量上,这样我就可以导出它,然后将其用作其他对象中的值。有人能帮我举个例子吗?我该怎么做?我只能在控制台上显示它 这是我的密码:React native 如何在React Native中导出和导入用户Id,react-native,api,React Native,Api,我需要从API中获取用户ID,然后将其附加到一个变量上,这样我就可以导出它,然后将其用作其他对象中的值。有人能帮我举个例子吗?我该怎么做?我只能在控制台上显示它 这是我的密码: class LoginScreen extends Component { constructor(){ super(); this.state = { email: '', password: '',
class LoginScreen extends Component {
constructor(){
super();
this.state = {
email: '',
password: '',
id: 0,
result: false,
}
}
_userLogin() {
let email = this.state.email;
let password = this.state.password;
if (email && password) {
fetch("https://URL/api/login", {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: email,
password: password,
})
})
.then((response) => {
if(response.status !== 200) {
throw new Error('The given data was invalid.')
}
return response.json();
})
.then((responseData) => {
this.renderResults(responseData)
console.log(responseData)
this.props.navigation.navigate('IntroScreen');
})
.catch((err) => console.log(err.message))
}
}
componentDidMount () {
fetch("https://URL/api/login", {
method: "GET",
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({getId: responseJson});
console.log(responseJson.data.id);
const id = responseJson.data.id;
})
.catch((error) => {
console.error(error);
});
}
renderResults = (responseData) => {
if(responseData){
this.setState({
result: true
})
}
}
handleEmail = (text) => {
this.setState({ email: text })
}
handlePassword = (text) => {
this.setState({ password: text })
}
errorMessage = () => {
Alert.alert(
'Error',
'The given data was invalid.',
[
{
text: "OK",
style: "cancel"
},
],
{ cancelable: false }
)
}
render() {
return (...)
将其附加到变量后,我希望在其他对象中使用它,如:
class HomeScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
currentDate: new Date(),
markedDate: moment(new Date()).format("YYYY-MM-DD"),
isLoading: true,
id: 1, <------- HERE
name: '',
floor: 0
};
}
类主屏幕扩展React.Component{
建造师(道具){
超级(道具);
此.state={
currentDate:新日期(),
markedDate:moment(new Date())。格式(“YYYY-MM-DD”),
孤岛加载:是的,
id:1,这里有一个快速的代码片段来修复您的问题
创建一个单例类gloablParams.js,如下所示
class GlobalParams {
constructor() {
this.id = null;
}
userId = (id)=>{
if(!id) return this.id;
this.id = id;
}
}
const instance = new GlobalParams()
export default instance;
现在在LoginScreen
中,您可以像这样导入它
import gp from './gloablParams';
现在在登录成功后调用这个函数,我认为它的组件已经挂载
或\u userLogin
并提供id来保存它
componentDidMount() {
fetch("https://URL/api/login", {
method: "GET",
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({ id: responseJson.data.id });
gp.userId(responseJson.data.id); //save it here
console.log(responseJson.data.id);
})
.then(gp.userId(id))
.catch((error) => {
console.error(error);
});
}
现在,您可以像我上面写的那样,以相同的方式导入它,并从相同的函数访问userid,但不带参数
gp.userId() //without any arguments
你可以使用redux或使用singleton类并将其保存在那里,然后在你的应用程序中随处使用。我如何使用class来实现它。我已经尝试过了,但没有成功。这样做怎么样?从“/des”导入UserId并像id:UserIdNo那样使用它,这不是实现它的正确方法。如果你注入在你的屏幕上没有正确显示。但是当我这样做时,我没有显示任何内容,甚至我在()中输入了“id”。你的主屏幕是在登录屏幕之前安装的吗?不,在登录屏幕之后安装的吗