S
SolidJS7mo ago
joenano

Computations created outside a render will never be disposed.

Just wondering whether I can ignore this warning or if its potentially a problem. Basically I have some tab components that are being created in a function and managed in a signal. When a tab is opened I add it to the array and when its closed I remove it from the array.
interface Tab {
index: number;
id: string;
title: string;
content: JSXElement;
}

const [selectedTab, setSelectedTab] = createSignal<Tab>();
const [tabs, setTabs] = createSignal<Tab[]>([], { equals: false });

export function addTab(event: Event, tab: Tab) {
setTabs((current) => {
current.push(tab);
return current;
});
}

export function closeTab(tab: Tab) {
const index = tab.index;

// update indexes of proceeding tabs
for (let i = index + 1; i < tabs().length; i++) {
tabs()[i]!.index--;
}

// remove tab from tabs array
setTabs((current) => {
current.splice(index, 1);
return current;
});
}
interface Tab {
index: number;
id: string;
title: string;
content: JSXElement;
}

const [selectedTab, setSelectedTab] = createSignal<Tab>();
const [tabs, setTabs] = createSignal<Tab[]>([], { equals: false });

export function addTab(event: Event, tab: Tab) {
setTabs((current) => {
current.push(tab);
return current;
});
}

export function closeTab(tab: Tab) {
const index = tab.index;

// update indexes of proceeding tabs
for (let i = index + 1; i < tabs().length; i++) {
tabs()[i]!.index--;
}

// remove tab from tabs array
setTabs((current) => {
current.splice(index, 1);
return current;
});
}
function loadSire(event: Event, title: string, sireId: number) {
const id = `sire-${sireId}`;

const content = <Content id={id} content={<Sire id={sireId} name={title} />} />;

const index = tabs().length;

addTab(event, { index, id, title, content });
}
function loadSire(event: Event, title: string, sireId: number) {
const id = `sire-${sireId}`;

const content = <Content id={id} content={<Sire id={sireId} name={title} />} />;

const index = tabs().length;

addTab(event, { index, id, title, content });
}
Is the memory handled by the garbage collector when I remove it from tabs array or is the warning related to something else im missing?
8 Replies
lxsmnsyc
lxsmnsyc7mo ago
loadSire's content is created outside of a root, hence the error I'd suggest rethinking loadSire so that the JSX stays in the JSX, and not separate from it
joenano
joenano7mo ago
I know thats where the warning is coming from, but was hoping it could be ignored as im keeping track of them
lxsmnsyc
lxsmnsyc7mo ago
for example:
function loadSire(event: Event, title: string, sireId: number) {
const id = `sire-${sireId}`;

const index = tabs().length;

addTab(event, { index, id, title });
}

<For each={tabs()}>
{({ index, id, title }) => <Content id={id} content={<Sire id={sireId} name={title} />} />}
</For>
function loadSire(event: Event, title: string, sireId: number) {
const id = `sire-${sireId}`;

const index = tabs().length;

addTab(event, { index, id, title });
}

<For each={tabs()}>
{({ index, id, title }) => <Content id={id} content={<Sire id={sireId} name={title} />} />}
</For>
joenano
joenano7mo ago
wouldnt that rerender every time a new tab is added tho? the point is to avoid rerendeing as there is a lot of data in each tab
lxsmnsyc
lxsmnsyc7mo ago
No it wouldn't.
joenano
joenano7mo ago
ill give it a try just now actually going to be a bit of a pain to change as different content in the first two tabs im not understanding why there wouldn't be a rerender tho, doesn't the for loop rerun every time tabs() triggers? what I have current is this, so when it triggers there is no recreation of the content component:
<For each={tabs()}>{(item) => item.content}</For>
<For each={tabs()}>{(item) => item.content}</For>
lxsmnsyc
lxsmnsyc7mo ago
doesn't the for loop rerun every time tabs() triggers?
Nope, that only happens when you do tabs().map(...). For compares each item in the given list and does the following: - moves elements that changed index - inserts new elements - removes elements no longer in the array
joenano
joenano7mo ago
ah thats cool, will spend some time reworking it today then Followed your advice and it worked, no more warning, and no rerenders. Thanks
Want results from more Discord servers?
Add your server
More Posts
Using Show with createRouteData / useRouteDatCurrently have a project setup with SolidStart, renderStream, and using createRouteData and useRoute1. arguments to useTransition() 2. measuring async updatesquicker one this time :) 1. looking at the official docs for useTransition: ``` import { useTransitiCan't run npm installHowdy y'all, I was trying to use the installer for solid start, and I chose the AuthJS option duringHow do we configure babel proposal decorators with Solid's vite plugin?I've got this Stackblitz: https://stackblitz.com/edit/solidjs-templates-wyjc1i?file=index.html,src%Can't build SolidStart after updating packagesAfter updating packages, I can't build project both locally and on vercel. I can't find anything relhow to trigger the fetcher in createResource if there are multiple signal values?hello, newbie here. In the document, it could be create a Memo as a group of signals as following. WNicest way of having computed properties in a store?I'm trying to model my store in a concise way, I want to have some base properties, and some computTypesafe way to render conditionally based on whether array has zero, one or many elements?Given an array with one or more elements, the idea is to display different UIs. With React, this canHow to implement JWT authentication in SolidJS, without SolidStart?I'm currently learning SolidJS for a SPA app with JWT-based auth. Right now I'm trying to implement Attempting to access a stale value from <Show>...Hi peeps, I have a rather complex application with lots of stores and recently have this issue. ``