Firstly, what is a cramp? OK, obviously they are strong, involuntary muscle contractions that commonly occur during or shortly after hard exercise, or when cold. Most cyclists - but not all - get them in the quads, hamstrings and calves (or like me, in the feet!). They can really hurt, really suddenly.
One theory of "cause" is that as a muscle fatigues, the brain sends more and more signals telling that muscle to contract to get the same strength of contraction, ie to get the desired result. The theory goes that when the muscle becomes utterly fatigued the brain sends a continuous contraction signal, which initiates a cramp. It could also be a protective mechanism. "I've reached my limit or I'm damaged", says the muscle, or the nervous system; and so it locks up so it can't be used. Great news if you are bike racing, eh, and the quads are calling it quits?
So, it's not good, but maybe we need to get 'em sometimes. But how do we avoid them?
Firstly, avoid muscle fatigue in the first place. Ask yourself, which muscles are prone to cramping? Can I shift the load more evenly?
Secondly, ask yourself when is it that you cramp? Is it toward the end of a long or hard ride, or only when racing? It may be that you have simply not trained adequately for the distance or the intensity of your riding. Some people cramp just before or during the sprint, for example. A 30km race is a lot more intense than a 30km training ride, so gradually up your training intensity. Upping the training distance may help push your limits as well. But make changes gradually, by increments, not in one huge leap!
You may cramp when pushing a big gear. Perhaps your cadence is too low? Try using lower gears to help get some spin back. Try not to drop below 85rpm. Whilst your endurance is measurably better at lower cadences the effort per rotation is greater. So strike a balance that offers easier pressure on the pedals by going for more spin.
Ask also if you are dehydrated, have depleted your electrolytes or have run out of energy. This is basic. Don't get dehydrated - you lose power and it may bring on cramps. And don't "bonk" (the cycling version of 'hitting the wall') or run out of energy. Keep eating carbohydrate rich foods during your rides. Your needs will vary with intensity and training, but 1-2 energy bars or satchels every 25km seems to be enough for my purposes. Certainly eat at the start of the ride, but not so much that you want to vomit when the racing starts! You are just topping up, not pigging out. Eat a main meal 2 hours before riding. This pattern of eating should also fix your electrolytes, but on a really hot day consider electrolyte-balanced sports drinks instead of water.
Other reputed causes of cramping include various medications and inappropriate supplementation. Regular stretching of muscles may reduce cramping (but for me that causes them!).And of course avoid the cold or any position thats cuts off your circulation. Stretching cold muscles almost always brings on a cramp for me; as does sitting in chairs with high support under the knee. I think it cuts off or limits blood flow around the hamstring... add in a fatigued muscle and whammo! Good luck!