简体   繁体   English

在反应中将变量从一个文件导出到另一个文件

[英]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.

相关问题 React - 如何将变量从一个文件获取到另一个文件 - React - how to get variable from one file to another 在 React Native 中将变量从一个文件传递到另一个文件 - Pass a variable from one file to another in React Native 如何在 React 中将变量名(不是完整的组件)从一个组件导出到另一个组件? - How do I export a variable name (not the full component) from one component to another in React? 如何在反应中将函数变量从一个文件传递到另一个文件 - how to pass function variable from one file to another in react 从 React 中的另一个 js 文件导出 function - Export a function from another js file in React React.js 如何从另一个没有导出的 js 文件中传递变量 - React.js How to pass a variable from another js file that does not have export 如何从另一个文件中导出钩子局部变量以及反应中的组件? - how to export a hook local variable from another file along with the component in react? React - 我有六个文件导出一个变量,并希望从一个文件中导出所有变量 - React - I have six files that export a variable and want all the variables to be exported from one file 如何将变量导出到另一个文件 React - How can I export a variable to another file React 在反应中将变量从一个组件发送到另一个组件 - Send variable from one component to another in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM