Skip to content

Commit ae8e203

Browse files
committed
gaurd for missing controller
1 parent 71f8764 commit ae8e203

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

src/components/Parallax/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ParallaxController } from 'parallax-controller';
22
import { useEffect } from 'react';
33

4-
export function useVerifyController(controller: ParallaxController) {
4+
export function useVerifyController(controller: ParallaxController | unknown) {
55
useEffect(() => {
66
const isServer = typeof window === 'undefined';
77
// Make sure the provided controller is an instance of the Parallax Controller

src/components/Parallax/index.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export function Parallax(props: PropsWithChildren<ParallaxProps>) {
1414

1515
function _getElementOptions(): CreateElementOptions {
1616
const useSpeedProp = typeof props.speed !== 'undefined';
17-
const isHorizontal = controller.scrollAxis == 'horizontal';
18-
const isVertical = controller.scrollAxis == 'vertical';
17+
const isHorizontal = controller?.scrollAxis == 'horizontal';
18+
const isVertical = controller?.scrollAxis == 'vertical';
1919

2020
let translateX = props.translateX;
2121
let translateY = props.translateY;
@@ -63,19 +63,23 @@ export function Parallax(props: PropsWithChildren<ParallaxProps>) {
6363

6464
// create element
6565
useEffect(() => {
66-
const newElement = controller.createElement(_getElementOptions());
66+
const newElement = controller?.createElement(_getElementOptions());
6767
setElement(newElement);
6868

69-
return () => controller.removeElementById(newElement.id);
69+
return () => {
70+
if (newElement) {
71+
controller?.removeElementById(newElement.id);
72+
}
73+
};
7074
}, []);
7175

7276
// update element
7377
useEffect(() => {
7478
if (element) {
7579
if (props.disabled) {
76-
controller.resetElementStyles(element);
80+
controller?.resetElementStyles(element);
7781
} else {
78-
controller.updateElementPropsById(
82+
controller?.updateElementPropsById(
7983
element.id,
8084
_getElementOptions().props
8185
);

src/hooks/useController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { useContext } from 'react';
22
import { ParallaxController } from 'parallax-controller';
33
import { ParallaxContext } from '../context/ParallaxContext';
44

5-
export function useController(): ParallaxController | {} {
5+
export function useController(): ParallaxController | null {
66
const parallaxController = useContext(ParallaxContext);
77
const isServer = typeof window === 'undefined';
88
if (isServer) {
9-
return {};
9+
return null;
1010
}
1111

1212
if (!parallaxController) {

0 commit comments

Comments
 (0)