createStore causing warning

I am using createStore for a lot of my app, to reduce prop drilling and make inter-component communication a lot easier. I will preface, my app works exactly as intended. No bugs or issues that i can see (currently) - however i get this warning due to my createStore structure.
computations created outside a `createRoot` or `render` will never be disposed
createComputation @ dev.js:748
computations created outside a `createRoot` or `render` will never be disposed
createComputation @ dev.js:748
This is an example store:
src/store/mdns/index.ts
import { createMemo } from 'solid-js'
import { createStore, produce } from 'solid-js/store'

export enum MdnsStatus {
ACTIVE = 'ACTIVE',
DISABLED = 'DISABLED',
LOADING = 'LOADING',
FAILED = 'FAILED',
}

interface IMdnsResponse {
ips: string[]
urls: string[]
}

interface IMdnsStore {
mdnsStatus: MdnsStatus
mdnsData: IMdnsResponse
}

export const defaultState: IMdnsStore = {
mdnsStatus: MdnsStatus.DISABLED,
mdnsData: {
ips: [],
urls: [],
},
}

const [state, setState] = createStore<IMdnsStore>(defaultState)

export const setMdnsStatus = (status: MdnsStatus) => {
setState(
produce((s) => {
s.mdnsStatus = status
}),
)
}

export const setMdnsData = (data: IMdnsResponse) => {
setState(
produce((s) => {
s.mdnsData = data
}),
)
}

export const mdnsState = createMemo(() => state)
import { createMemo } from 'solid-js'
import { createStore, produce } from 'solid-js/store'

export enum MdnsStatus {
ACTIVE = 'ACTIVE',
DISABLED = 'DISABLED',
LOADING = 'LOADING',
FAILED = 'FAILED',
}

interface IMdnsResponse {
ips: string[]
urls: string[]
}

interface IMdnsStore {
mdnsStatus: MdnsStatus
mdnsData: IMdnsResponse
}

export const defaultState: IMdnsStore = {
mdnsStatus: MdnsStatus.DISABLED,
mdnsData: {
ips: [],
urls: [],
},
}

const [state, setState] = createStore<IMdnsStore>(defaultState)

export const setMdnsStatus = (status: MdnsStatus) => {
setState(
produce((s) => {
s.mdnsStatus = status
}),
)
}

export const setMdnsData = (data: IMdnsResponse) => {
setState(
produce((s) => {
s.mdnsData = data
}),
)
}

export const mdnsState = createMemo(() => state)
src/store/mdns/selectors.ts
import { createMemo } from 'solid-js'
import { mdnsState } from './mdns'

export const mdnsStatus = createMemo(() => mdnsState().mdnsStatus)
export const mdnsData = createMemo(() => mdnsState().mdnsData)
import { createMemo } from 'solid-js'
import { mdnsState } from './mdns'

export const mdnsStatus = createMemo(() => mdnsState().mdnsStatus)
export const mdnsData = createMemo(() => mdnsState().mdnsData)
7 Replies
DaOfficialWizard🧙
Essentially i have my getters in selectors.ts and my setters in the index.ts for that store. I am creating these objects outside of a createRoot and a render inside of some files, then i import the getters and setters through out my app as i need them. The store acts as a "global cache" for my project - where needed. What is the proper way to do this, instead?
REEEEE
REEEEE•2y ago
You could use a context instead
DaOfficialWizard🧙
that seems overly cumbersome tbh I tried a context it felt bloated and was a pain to work with
REEEEE
REEEEE•2y ago
Tbh, your method is fine otherwise I think
DaOfficialWizard🧙
so iam not leaking memory or something? I mean, i haven't seen it in the app profiling - but i just figured i might be doing something that could affect performance.
REEEEE
REEEEE•2y ago
From what I understand, if it's a SPA then all the stuff would be disposed of anyway once the tab is closed There is another way to do it, since it has been asked before, but I can't remember it off the top of my head at the moment
DaOfficialWizard🧙
yeah, it is. No ssr or anything special. Just a tauri app and this is the front-end logic.
Want results from more Discord servers?
Add your server