英文:
Pass a variable from one file to another in React Native
问题
我正在使用React Native构建一个前端应用程序。一个文件(屏幕)使用以下代码生成一个令牌。
在另一个文件中,我必须使用该令牌进行验证。
我不知道如何在这两个文件之间传递令牌。
英文:
I'm building a front end app on React Native. One file (screen) is generating a Token using the following code
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
fetch("http://192.168.1.11:8080/api/login", {
method: 'POST',
body: JSON.stringify({
username: 'user'
}),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
}).then((response) => response.json())
.then((json) => {
this.setState({ JwToken: json.token });
}).then((json) =>this.props.navigation.navigate('Home')/)
.catch((error) => console.error(error))
.finally(() => {
this.setState({ isLoading: false });
});
<!-- end snippet -->
In another file I must use that token for verification
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
fetch('http://192.168.1.11:8080/api/books', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + JwToken
},
body: JSON.stringify({
title: 'new book',
price: 4
})
});
<!-- end snippet -->
I had no idea how to pass the token between the two files.
答案1
得分: 1
这是我以前从React Navigation 文档中引用过的内容,似乎符合您的需求。
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => {
/* 1. 使用参数导航到 Details 路线 */
navigation.navigate('Details', {
itemId: 86,
otherParam: '你想要的任何内容',
});
}}
/>
</View>
);
}
function DetailsScreen({ route, navigation }) {
/* 2. 获取参数 */
const { itemId } = route.params;
const { otherParam } = route.params;
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Text>itemId: {JSON.stringify(itemId)}</Text>
<Text>otherParam: {JSON.stringify(otherParam)}</Text>
<Button
title="再次前往详情页"
onPress={() =>
navigation.push('Details', {
itemId: Math.floor(Math.random() * 100),
})
}
/>
<Button title="前往首页" onPress={() => navigation.navigate('Home')} />
<Button title="返回" onPress={() => navigation.goBack()} />
</View>
);
}
英文:
This is something I've referenced before from the React Navigation Docs that seems to fit your needs.
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => {
/* 1. Navigate to the Details route with params */
navigation.navigate('Details', {
itemId: 86,
otherParam: 'anything you want here',
});
}}
/>
</View>
);
}
function DetailsScreen({ route, navigation }) {
/* 2. Get the param */
const { itemId } = route.params;
const { otherParam } = route.params;
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Text>itemId: {JSON.stringify(itemId)}</Text>
<Text>otherParam: {JSON.stringify(otherParam)}</Text>
<Button
title="Go to Details... again"
onPress={() =>
navigation.push('Details', {
itemId: Math.floor(Math.random() * 100),
})
}
/>
<Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
<Button title="Go back" onPress={() => navigation.goBack()} />
</View>
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论