How to send data between different pages using Solid js?
Hello! I need to send user's phone number between 2 different pages. I know I could use a browser's Session Storage to do that but I'm not sure it is a good practice. Is there smth like VueX in vue.js or other ideas?
45 Replies
You can create a context provider that will allow for you to store that state in a component high in the component tree, and then you can reference that context from any component below it.
SolidJS
Solid is a purely reactive library. It was designed from the ground up with a reactive core. It's influenced by reactive principles developed by previous libraries.
Stores and signals can also be created globally and imported into a component.
So you can probably just create and export that store from outside that component
and then you can import just the store
I tried creating store globally but I failed to get any data to target component
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Don't create the state within loginStore
because it recreates the phoneState every time it's called
This should work
And then you can simply import the exported variables within your other components
Like this:
And I guess I can just use CreateSignal here
Yeah
If you're only storing the phone number, there's no point wrapping it in a store
If you're planning on adding more global state, a context provider might be a better option.
That's not working -- the same result.
It's working just on the same page where a signal is created
Are you able to recreate the issue in a solid playground?
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Ok. I'll try
Is there any error message that shows up when you try to access the signals from somewhere else?
Nope
Can you show me the code for both files?
What's the other file?
Sorry, my mistake
console.log shows that phoneNumber is empty
undefined?
or just empty?
empty
If I put phoneNumber()
But if I write just getter obj I get
Looks like it comes empty with import
Maybe try putting the signal in a different file altogether
If not, you can always go the context provider route
I suspect it's because the Login file reloads at some point
So the state within it gets lost
You can test it by placing a console.log at the root level in the file, where the signal is created
ok, I'll try. Thank you!
I have known a lot today!
I'm new bee in Solid.js I used to use Vue and it's completely different
Yeah, I'm also pretty new to solid
Its behaviour can sometimes get inconsistent in my experience
May be it reloads because of router?
It probably gets unmounted
You can rectify it by simply declaring the signal outside of the router
Like in a new file, for example
You could probably pass the signal down as props if you wanted
So you could create it in the main file
Yeah! It's quite challenging but interesting.
Definitely, its model is super different to pretty much anything else
No VDOM definitely makes it harder to work with though
Unfortunately I have Solid in production now and I struggle.
That's how I made it work. I guess the dedicated independent routes drops their state. So when I had rewritten the Router like nested it got worked
Awesome
Do you need to access that phone number anywhere else though?
Because then the state would probably be lost in the same way
Yes, that's a right question. It loses the phoneNumber once I reload the page. May be using the context provider solve this problem.
If you want data to persist upon page reload, you'll need to put the number into local storage
You can use a context provider to do that, with an effect on the phonenumber signal that updates localStorage upon change
So I just could use browser's Session storage without using signals at all :)))
You could
But it's generally not a great idea
Mostly because localStorage isn't reactive
I just wanted phoneNumber not to be seen out of app's runtime. So I use signals here. I guess I don't need the reactivity in this case.
1. User fill in the phone
2. I check If there is a user with such unique phone
3. If not I inform them and suggest to sign up
So there is not the case where the initially typed phone number could be changed somehow
How are you persisting their login state?
I haven't implemented that feature yet
You'll definitely need some sort of username and password based login if you want it to be secure
or Oauth
That's my first project where I need to implement it
I have a backed based on JWT + cookie
And It takes Username and Password
Oh, awesome
That's good
What's the phone number for?
Phone number is used as username and my boss want it to be confirmed before it will be written into db
Not as username to be more accurate -- as login
So I have something like pre-signup logic here
Yeah, a context provider is probably a bit overkill for persisting data between signup and login pages
It's definitely worth using for storing user data though
Once they're logged in
That's what I 'm thinking about right now😆
But it's was very useful to learn how signals and stores can be used
And even Routers
awesome
good luck with your app
Thank you a lot. I'm glad to meet you here. Best wishes to you in your projects!