I have read a few times about people reciting the shema before dying.
what is the reason for this and does it have any basis in halacha.
Mi Yodeya is a question and answer site for those who base their lives on Jewish law and tradition and anyone interested in learning more. It only takes a minute to sign up.Sign up to join this community
Rabbi Akiva patiently endured while his flesh was being torn with iron combs, and died reciting the Shema. He pronounced the last word of the sentence, Eḥad ("one") with his last breath (Talmud Berachot 61b). Since then, it has been traditional for Jews to say the Shema as their last words.
It doesn't state that this is the basis for the custom, but, it certainly would lend support to the custom. If you read the story in Brachot, Rabbi Akivah cites the words בכל נפשך - "With all your soul" and the Gemarah, there, explains "even if your soul is about to be taken", you should love G-d.
IMO, this seems to lend strong support for the reason of reciting Shema just prior to death. I don't know if it is customary to recite the paragraph "Ve'ahavta". But, from the story with Rav Akiva, it says that he was killed at the time of reciting Kri'at Shema, so, had it not been for the fact that he died "too soon", for all we know, he may have reached that verse.
Basis is the death of Yaakov described in Pesachim 56a although over there his kids said Shema and he said Baruch Shem.