When sodium reacts with water, the following chemical reaction takes place:
`2 Na(s) + 2H_2O (l) -> 2NaOH (aq) + H_2 (g) + heat`
The sodium atoms react with water in an exothermic reaction to form sodium hydroxide and hydrogen gas. Heat is also released in this process.
Similarly, when magnesium reacts with water, the following chemical reaction takes place:
`Mg(s) + 2H_2O (l) -> Mg(OH)_2 (aq) + H_2 (g)`
Magnesium atoms do not react as readily with water as sodium atoms. Magnesium atoms react with hot water to form magnesium hydroxide and hydrogen gas.
Sodium and magnesium are metals and are capable of forming ionic bonds. The atoms of these metals lose electrons to form cations, which form ionic bonds with the hydroxide anions of water molecules. In this process, metal atoms lose electrons and hydroxide anions gain them and all the cations and anions obtain fully-filled electronic configurations.
Hope this helps.