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