Flexing your foot as you feel one coming on can stop it from happening or get you out of one. The flexed foot stretches your calf muscle.
I can’t emphasize enough to get your vitamin D level checked, and if it’s low take supplements to get the level up into normal range. The reason I emphasize it is because a lot of doctors ignore or don’t believe in a relationship between D and muscle weakness and cramps, but if you come back with your D at 20, when it should be above 32 then your doctor will help you track how much D you are taking and of it is effectively getting you into the normal range. Maybe you have already been tested recently? If so, check your number. By the way it was a doctor who first tested me, and I doubted it had anything to do with my cramps. Now, I know she was right.
Your doctor will likely test your electrolytes like sodium and potassium, which are important to be in normal range to avoid muscle cramps also, but if you always have those numbers normal, eating a banana isn’t likely to help, and I refer back to vitamin D.
Staying well hydrated is important, but during sleep that isn’t easy. If your sodium level is low you might be losing a lot of water.
While you’re awake your foot is flexed most of the time helping to avoid the cramp.
Lastly, Magnesium is well absorbed through the skin, there is a such thing as too much magnesium, so don’t over do. You can use Epson salts or buy magnesium oil spray, and spray directly on the muscle a few times a week. I find the spray works well for some immediate relief.
You can get your magnesium level tested also, blood test, if you’re curious. Most people are in normal range. Vitamin D a different story, something like 35% of the US population is deficient, and it’s very common even in sunny places like your state. We who live in the sun usually protect ourselves from the sun the most.