Most of the time...the gnat eggs was in the soil you bought. Happens to me all the time...

.
I have read that, covering the top of the soil with sand or a paper plate/foil or something will stop more eggs from hatching. (not advice, just something I read)
Me?..I just kill them...(amono~amono)...hand to hand combat...

..I don't think they hurt the plants; but not sure.