Skip to content

Commit

Permalink
fix: create controller only once (#235)
Browse files Browse the repository at this point in the history
- related to strict mode issues #233 #227 and #221
- credit to @raRaRa for #234
  • Loading branch information
jscottsmith authored Nov 21, 2023
1 parent f2e1719 commit 61d56cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/components/ParallaxProvider/ParallaxProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import React, { PropsWithChildren, useEffect, useRef } from 'react';

import { ParallaxContext } from '../../context/ParallaxContext';
import { ScrollAxis } from 'parallax-controller';
import { ParallaxController, ScrollAxis } from 'parallax-controller';
import { ParallaxProviderProps } from './types';
import { createController } from './helpers';

export function ParallaxProvider(
props: PropsWithChildren<ParallaxProviderProps>
) {
const controller = useRef(
createController({
const controller = useRef<null | ParallaxController>(null);

if (!controller.current) {
controller.current = createController({
scrollAxis: props.scrollAxis || ScrollAxis.vertical,
scrollContainer: props.scrollContainer,
disabled: props.isDisabled,
})
);
});
}

// update scroll container
useEffect(() => {
Expand All @@ -37,7 +39,6 @@ export function ParallaxProvider(
useEffect(() => {
return () => {
controller?.current && controller?.current.destroy();
controller.current = null;
};
}, []);

Expand Down
16 changes: 14 additions & 2 deletions src/components/ParallaxProvider/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ParallaxController } from 'parallax-controller';
import { render } from '@testing-library/react';
import { ParallaxProvider } from '.';
import { useParallaxController } from '../../hooks/useParallaxController';
import * as helpers from './helpers';

describe('A <ParallaxProvider>', () => {
it('to render children', () => {
Expand Down Expand Up @@ -48,6 +49,15 @@ describe('A <ParallaxProvider>', () => {
expect(parallaxController).toBeInstanceOf(ParallaxController);
});

it('calls to createController only once', () => {
jest.spyOn(helpers, 'createController');
const { rerender } = render(<ParallaxProvider />);
rerender(<ParallaxProvider />);
rerender(<ParallaxProvider />);
rerender(<ParallaxProvider />);
expect(helpers.createController).toHaveBeenCalledTimes(1);
});

it('to disable parallax elements and re-enable', () => {
let parallaxController: ParallaxController | null = null;

Expand Down Expand Up @@ -100,8 +110,10 @@ describe('A <ParallaxProvider>', () => {
);

screen.unmount();
// @ts-expect-error
expect(parallaxController?.destroy).toBeCalled();

expect(
(parallaxController as unknown as ParallaxController)?.destroy
).toBeCalled();
});

it('to update the scroll container when receiving a new container el', () => {
Expand Down

1 comment on commit 61d56cc

@vercel
Copy link

@vercel vercel bot commented on 61d56cc Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.