Cramps are a complicated subject, but the usual culprit is (as you state) hydration and electrolytes.
These are used for transmission of electrical signals though our body, when we don't have enough the system malfunctions, some things we take depletes out body of these necessary materials.