[英]Export variable from one file to another in react
I am currently working on a react-native based website and I am struggling to pass values from one file to another.我目前正在开发一个基于 react-native 的网站,我正在努力将值从一个文件传递到另一个文件。 The value I am trying to import is 'Key':我要导入的值是“键”:
class Encrypt extends Component {
encryptData = async() => {
fetch('http://localhost:7000/Data')
.then(res => res.json())
.then((data) => {
var pub1Key = data ;
var Key=pub1Key["encryptedData"];
}
And use it like this并像这样使用它
class Decrypt extends Component {
deData(){
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({data: Key })
};
I tried to use export option but it didn't work.我尝试使用导出选项,但它不起作用。 These are two separate files.这是两个单独的文件。 Encrypt.js and Decrypt.js.加密.js 和解密.js。
You can manage this via any state manager.您可以通过任何 state 管理器进行管理。 You can use Redux or Mobx or ReactContext for that.您可以为此使用 Redux 或 Mobx 或 ReactContext。
However, if this is the token that is required for authentification or data that you want to access even after the page will be reloaded, you can simply store it in AsyncStorage.但是,如果这是身份验证所需的令牌或您希望在重新加载页面后访问的数据,您可以简单地将其存储在 AsyncStorage 中。
So your code would look something like that.所以你的代码看起来像这样。
import {AsyncStorage} from 'react-native';
class Encrypt extends Component {
encryptData = async() => {
fetch('http://localhost:7000/Data')
.then(res => res.json())
.then((data) => {
//you can decrypt 'data' here
return AsyncStorage.setItem('pub1Key', data);
})
.catch((err)=>{
//handle any errors here
})
}
class Decrypt extends Component {
async getData(){
return new Promise((resolve, reject)=> {
try{
const data = await AsyncStorage.setItem('pub1Key', data);
//you can encrypt data here
resolve(data);
} catch(e){
reject(e);
}
})
};
The docs for that.为此的文档。 https://reactnative.dev/docs/asyncstorage.html https://reactnative.dev/docs/asyncstorage.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.