I know what you mean - I’ve thought it strange how my legs are normally so weak, but then can suddenly move with a lot of force. I’ve found I get different types of spasm in different parts of my legs, and I’ve found tricks that help with most of them. Sometimes it’s caused by temperature - either my feet are too hot too cold, in which case it’s a simple case of coolng them down (by putting them under a cold shower or using Deep Freeze gel) or warming them up (with a pair of thick socks or by rubbing them).
My most annoying one is accompanied by a particular feeling in my heels, and I discovered by chance that if I lie on my front with my legs out straight, that always fixes it. Another painful one I get is linked to my bladder, and I know that if I just empty my bladder it will stop (this one often happens when I’ve drunk something that will irritate the bladder, like too much caffeine.)
Another I get is helped by stretching my calf. And sometimes it can be caused by having been on my feet too much. Then some gentle massaging and stretching is best.
Something which I’ve noticed can make it worse is if I get annoyed at my legs for spasming. Instead I sort of mentally soothe my legs, or tell them they have permission to feel what they’re feeling. It’s a bit like having a baby that’s crying - get annoyed & stressed with it for crying, and it will probably cry even more. But being soothing to my legs helps them to calm down more. If, for example, I know the spasm is because I’ve done a lot of walking & standing, I congratulate & thank my legs for doing a good job, and say it’s unsurprising they feel tired and they just need to rest. That probably sounds a bit bonkers, but it seems to help. At the very least, I feel much calmer about it.
Hope you find the right thing to soothe your legs.