mirror of https://github.com/msgbyte/tailchat
feat: add <LoadingOnFirst />
parent
96fddef56f
commit
60339fda39
@ -0,0 +1,34 @@
|
||||
import React, { useMemo, useRef } from 'react';
|
||||
import { Loading, LoadingProps } from './Loading';
|
||||
|
||||
interface LoadingOnFirstProps extends LoadingProps {
|
||||
spinning: boolean;
|
||||
className?: string;
|
||||
style?: React.CSSProperties;
|
||||
}
|
||||
/**
|
||||
* 类似于 <Loading /> 但是只会触发一次
|
||||
*/
|
||||
export const LoadingOnFirst: React.FC<LoadingOnFirstProps> = React.memo(
|
||||
(props) => {
|
||||
const lockRef = useRef(false);
|
||||
const spinning = useMemo(() => {
|
||||
if (lockRef.current === true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (props.spinning === true) {
|
||||
lockRef.current = true;
|
||||
}
|
||||
|
||||
return props.spinning;
|
||||
}, [props.spinning]);
|
||||
|
||||
return (
|
||||
<Loading {...props} spinning={spinning}>
|
||||
{props.children}
|
||||
</Loading>
|
||||
);
|
||||
}
|
||||
);
|
||||
LoadingOnFirst.displayName = 'LoadingOnFirst';
|
Loading…
Reference in New Issue