Increment counter every time component is used?
Currently I use context to to have signals for this counters but is is possible to get this without context or imported signal?
<Thing/>
<Thing/>
<Thing/>
First one would have 1 and last one 3 and so on
7 Replies
const compCreationCount = { thingComponent: 0 }
const Thing = (props) {
const iAmIncarnationNumber = compCreationCount.thingComponent++
return <p>me is: {iAmIncationNumber} </p>
}
What do you need a signal and a context ?
side thought.. Definitely this commnity needs a sticky on Context abuse somewhere
Thanks, I was thinking I could maybe do it like that with variable outside component in file.
In hindsight my solution is stupid, but I guess i just thought compcreationcount is initialized every time component is rendered. Well it's learning exp.
I think its easy to overthink in any framework, part of the joy of novelty, let's play the whole API surface, when reality simple primitives can work / do work better
we don't because we recommend the use of it specially when working with SSR
my question is what was the purpose of the number
if it's for id generation, usually createUniqueId just works
I need it for chartistjs id, basically i use the number for that
But createuniqueid seems to work for that too.
this was antipattern after all and i didnt even know the existence of createuniqueid.
Kind of difficult to know all the best practices
Are signals in global space in file good practice either in SSR context?
you don't really want any global state in ssr because it'll be shared between app instances on the server
Ah okay, yeah my opinion based on client-side app only, I've not played with the server side story yet