SolidJSS
SolidJSโ€ข3y agoโ€ข
6 replies
Mathieu

Variable initialized in `onMount` becomes `undefined`

let positionMenu: () => void;

onMount(() => {
  positionMenu = getPositionMenuFun({ inputElement, styles });
  console.log('positionMenu', positionMenu); // always outputs the function
});

createEffect(() => {
  // ...
  positionMenu(); // error: in one scenario `positionMenu` is undefined
});


I initialize a variable returning a function in the onMount callback, then call it in some effect. In one scenario positionMenu will be
undefined
. It is nowhere else in the code (only these occurrences of "positionMenu" as shown above).

I noticed it's due to a clean up but I do not understand how the variable loses its value.
Was this page helpful?