Skip to content

Commit

Permalink
refactor: 💡 change to use abstracted initDotlottiePlayer method
Browse files Browse the repository at this point in the history
  • Loading branch information
afsalz committed Sep 6, 2023
1 parent 148197c commit faddcb3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
12 changes: 8 additions & 4 deletions packages/react-player/src/hooks/use-dotlottie-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const useDotLottiePlayer = (
config?: DotLottieConfig<RendererType> & {
lottieRef?: MutableRefObject<DotLottieRefProps | undefined>;
},
): { dotLottiePlayer: DotLottiePlayer; setDotLottiePlayer: (player: DotLottiePlayer) => void } => {
): { dotLottiePlayer: DotLottiePlayer; initDotLottiePlayer: () => void } => {
const [dotLottiePlayer, setDotLottiePlayer] = useState<DotLottiePlayer>(() => {
return new DotLottiePlayer(src, container.current, config);
});
Expand All @@ -89,7 +89,11 @@ export const useDotLottiePlayer = (
dl.load();

return dl;
}, [container]);
}, [container, src, config]);

const initDotLottiePlayer = useCallback(() => {
setDotLottiePlayer(getDotLottiePlayer());
}, [getDotLottiePlayer]);

if (config?.lottieRef) {
useImperativeHandle(
Expand Down Expand Up @@ -227,7 +231,7 @@ export const useDotLottiePlayer = (
}

useEffectOnce(() => {
setDotLottiePlayer(getDotLottiePlayer());
initDotLottiePlayer();

return () => {
dotLottiePlayer.destroy();
Expand All @@ -236,6 +240,6 @@ export const useDotLottiePlayer = (

return {
dotLottiePlayer,
setDotLottiePlayer,
initDotLottiePlayer,
};
};
14 changes: 5 additions & 9 deletions packages/react-player/src/react-player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import type { RendererSettings, PlayMode } from '@dotlottie/common';
import { DotLottiePlayer as DotLottiePlayerCommon, PlayerState, PlayerEvents } from '@dotlottie/common';
import { PlayerState, PlayerEvents } from '@dotlottie/common';
import React, { useEffect, useRef } from 'react';
import type { MutableRefObject } from 'react';
import { useUpdateEffect } from 'react-use';
Expand Down Expand Up @@ -59,7 +59,8 @@ export const DotLottiePlayer: React.FC<DotLottiePlayerProps> = ({
...props
}) => {
const container = useRef(null);
const config = {

const { dotLottiePlayer, initDotLottiePlayer } = useDotLottiePlayer(src, container, {
lottieRef,
renderer,
activeAnimationId,
Expand All @@ -81,9 +82,7 @@ export const DotLottiePlayer: React.FC<DotLottiePlayerProps> = ({
defaultTheme,
light,
activeStateId,
};

const { dotLottiePlayer, setDotLottiePlayer } = useDotLottiePlayer(src, container, config);
});

const currentState = useSelectDotLottieState(dotLottiePlayer, (state) => state.currentState);
const frame = useSelectDotLottieState(dotLottiePlayer, (state) => state.frame);
Expand Down Expand Up @@ -183,10 +182,7 @@ export const DotLottiePlayer: React.FC<DotLottiePlayerProps> = ({

useUpdateEffect(() => {
if (typeof src !== 'undefined') {
const dl = new DotLottiePlayerCommon(src, container.current, config);

dl.load();
setDotLottiePlayer(dl);
initDotLottiePlayer();
}
}, [src]);

Expand Down

0 comments on commit faddcb3

Please sign in to comment.