简体   繁体   English

如何将变量导出到另一个文件?

[英]How to Export a Variable to Another file?

const ForgotPassword = ({ ...others }) => {
  const theme = useTheme();
  const { token } = useParams()

  const handleSubmit = async(values) => {
    console.log('load')
    try {
      const response = await axios.post(`http://localhost:3001/auth/reset/${token}`, {
        password: values.password
      });
      if (response.data.msg === 'Password reset token is invalid or has expired') {
        console.log(response)
      } else {
        // success message
      }
    } catch (err) {
      console.error(err);
      console.log("Something went wrong. Please try again later.")
    }
    console.log('not load')
  };


  return (
    //...Content...
  );
};

export default ForgotPassword;

I need to export token to use in other files how to do?我需要导出令牌以在其他文件中使用怎么办?
I tried like this我这样试过

export const token = useParams().token;

export const Token  = token 

etc.. ETC..

I'm new to this, could anyone tell me how to export token ?我是新手,谁能告诉我如何导出token

You can't export token directly because it's a local variable and is not available to export at the start of your app.您不能直接导出token ,因为它是一个局部变量,无法在您的应用程序启动时导出。 Depending on where token needs to be accessed, you can pass it to a child component with a prop.根据需要访问token的位置,您可以将其传递给带有 prop 的子组件。 If this fits your use case, it'll be the easiest method.如果这适合您的用例,这将是最简单的方法。

If you instead need to access the data from adjacent or parent components, you can set up an application store and access it from parent components.如果您需要从相邻或父组件访问数据,您可以设置一个应用程序商店并从父组件访问它。 Something like React Redux will do this for you.React Redux这样的东西会为你做这件事。 Take a look at the React Redux Quick Start page for details on how that might work.查看React Redux 快速入门页面,了解其工作原理的详细信息。 This is my preferred method, but you can also use React Context instead of an external library to accomplish the same task.这是我的首选方法,但您也可以使用React Context而不是外部库来完成相同的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将此变量从类方法导出到另一个文件? - How do I export this variable from class method to another file? 如何从一个文件导出变量,并在另一个文件上require()导出的变量? - How can I export a variable from one file and require() the exported variable on another file? 将功能的特定变量导出到另一个文件 - Export the particular variable of function to another file 如何将变量导出到另一个反应功能组件? - How to export a variable to another react functional component? 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? 如何从另一个 html 文件导入变量? (我使用导入/导出,但出现错误) - How to import variable from another html file? (I use import/export, but error occurs) 如何从另一个文件流中导出内容? - How to export something from another file in flow? NodeJs:如何将异步值导出到另一个文件? - NodeJs: How to export an async value to another file? 如何将 python 变量导出为 javascript 文件 - How to export python variable as javascript file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM