Skip to content

fix: strict mode and missing context value #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/components/ParallaxProvider/ParallaxProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { PropsWithChildren, useEffect, useRef } from 'react';
import React, { PropsWithChildren, useEffect, useState } from 'react';

import { ParallaxContext } from '../../context/ParallaxContext';
import { ScrollAxis } from 'parallax-controller';
Expand All @@ -8,41 +8,44 @@ import { createController } from './helpers';
export function ParallaxProvider(
props: PropsWithChildren<ParallaxProviderProps>
) {
const controller = useRef(
const [controller] = useState(
createController({
scrollAxis: props.scrollAxis || ScrollAxis.vertical,
scrollContainer: props.scrollContainer,
disabled: props.isDisabled,
})
);

// update scroll container
useEffect(() => {
if (props.scrollContainer && controller.current) {
controller.current.updateScrollContainer(props.scrollContainer);
if (props.scrollContainer && controller) {
controller.updateScrollContainer(props.scrollContainer);
}
}, [props.scrollContainer, controller.current]);
}, [props.scrollContainer, controller]);

// disable/enable parallax
useEffect(() => {
if (props.isDisabled && controller.current) {
controller.current.disableParallaxController();
if (props.isDisabled && controller) {
controller.disableParallaxController();
}
if (!props.isDisabled && controller.current) {
controller.current.enableParallaxController();
if (!props.isDisabled && controller) {
controller.enableParallaxController();
}
}, [props.isDisabled, controller.current]);
}, [props.isDisabled, controller]);

// remove the controller when unmounting
// enable and disable parallax controller on mount/unmount
useEffect(() => {
// Enable it on mount
if (!props.isDisabled && controller) {
controller && controller?.enableParallaxController();
}
return () => {
controller?.current && controller?.current.destroy();
controller.current = null;
// Disable it on unmount
controller && controller?.disableParallaxController();
};
}, []);

return (
<ParallaxContext.Provider value={controller.current}>
<ParallaxContext.Provider value={controller}>
{props.children}
</ParallaxContext.Provider>
);
Expand Down