S
SolidJS11mo ago
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
});
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.
6 Replies
apollo79
apollo7911mo ago
Can you give a reproduction? In the code above, it seems to work so something is probably missing
thetarnav
thetarnav11mo ago
How do you know if the effects run from top to bottom?
Martnart
Martnart11mo ago
Might be too obvious (I guess you have checked that off already) but effects also run before mounting. so it's initially undefined.
thetarnav
thetarnav11mo ago
GitHub
solid/packages/solid/src/reactive/signal.ts at f79ba4d9087d74ef7e55...
A declarative, efficient, and flexible JavaScript library for building user interfaces. - solidjs/solid
Martnart
Martnart11mo ago
I meant that an effect returned from createEffect will execute before onMount Edit: Need to add that I am talking about an SSR context. Might well be not relevant for this particular use-case.
thetarnav
thetarnav11mo ago
probably the best way to ensure that the effect will have the variable available is to nest the effect insie onMount:
onMount(() => {
const positionMenu = getPositionMenuFun({ inputElement, styles });

createEffect(() => {
positionMenu();
});
});
onMount(() => {
const positionMenu = getPositionMenuFun({ inputElement, styles });

createEffect(() => {
positionMenu();
});
});
Want results from more Discord servers?
Add your server
More Posts
Cannot build on serverI have a server (debian bookworm) and my local PC (Arch) My site (https://github.com/someaspy/duckduSolid-Element Custom Elements ManifestIf I'm using the Solid-Element library to generate web components, is there a way to generate a [cusWhat's the SEO situation like with solid?As far as i know MPA is better for SEO but I've not been able to find any information on it. As I knCan't start the server with https enabled on StartI have a certificate and I changed the vite.config.ts to use it by adding ```ts server: { https: { Jest, TypeScript, and @solidjs/routerI set up my project with Jest & TypeScript according to https://www.solidjs.com/guides/testing#jest-Any pattern to avoid getting ts angry when using route data fields?I am using routeData returning an object with 2 fields from it. When I use a combination of <Show> aIs there some equivalent to <Portal/> that is also compatible with ssr?I want to make a persistent modal dialog, and for that i need the `<Portal/>` component from `solid-Recommended way to track the setter of a signal?Currently, my solution involves wrapping the setter with a function. Playground: https://playground.Is it possible to pass paramters a function wrapped by a createEffect/createMemo?,I have a certain function in my component which I have noticed runs multiple times since I am referIs it possible to implement LiveViewJS type functionality using SolidJS + SSR?Specifically, simple example would be : an Observable (for example from rxjs or S.js ) keeps updatin