简介
可以包裹在组件外面,来判断是否登陆,如果没有登陆,即可跳转登陆页面,
具体代码实现如下:
- 公用组件
import React from 'react';
import type { ElementType } from 'react';
import Taro, { showModal } from '@tarojs/taro';
const withSignIn = (WrappedComponent: ElementType) => {
class HocComponent extends React.Component {
state = {};
async componentDidShow() {
const storageResp = await Taro.getStorage({ key: COOKIES });//登陆后存起来的信息
const cookies = storageResp?.data;
if (!cookies?.length) {
const res = await showModal({
content: '请先登录',
showCancel: false,
});
if (!res?.confirm) {
return;
}
await Taro.navigateTo({
url: '登陆文件的地址',
});
}
}
render() {
return <WrappedComponent {...this.props} {...this.state} />;
}
}
return HocComponent;
};
export default withSignIn;
使用组件
import { View } from '@tarojs/components';
const Page = () => {
return (
<div>page</div>
);
};
export default withSignIn(Page);