Not sure whether I'm using reactivity right... (Codereview)
Ok so I recently ported my personal project to Solid coming from React, and while I did it I kind of translated anything React into it's Solid alternative, in this case something didn't quite work out. now, I actually got it to work but I'm not sure I'm doing it the right way and I definitely don't know what I'm doing, which is why I'm here.
Here's the code, with an explanation below:
9 Replies
So basically the way I had it before was something on these lines: On my Sidebar component, I would have some buttons that would trigger a navigation, using useNavigate(). I actually switched to some <A /> tags and I'd like to hear about that too, but that shouldn't really matter I think. Anyway, then, In this component, which is the Header component, I would implement useLocation() to get the current path in order to update the text, by mapping it to every possible route. Now, in React that worked for some reason I actually don't quite understand, but intuitively useLocation probably knew when it was changing and triggered a rerender and stuff. Now, in Solid, i just switched the useLocation by React to the useLocation of Solid, and had it like this:
But that didn't work, probably because I didn't implement any form of tracking.
That's it, lemme know what you think of my new implementation :)
You just have to make them functions
what do you mean?
path and header should be functions
So that they can be reactive
oh like just do something on the lines of this?
Yup
like just this would work?
why the heck do signals and effects and stuff exist then
the reason it works is because
pathname
is a signal internally if you look at the source. Variables on their own are not reactive like you had in the original post
when you make a something a function like this and use a signal inside of it, it's called a derived signal. If you use that function anywhere, and the signal used inside the function changes, so will this functions output if anyOh ok
Thanks
Very clear explanation too