Imaam ibn Rajab al-Hanbali (rahimahullah) says, “And know that the deeds for other than Allah are of different types:
The first type
Sometimes it is purely to show off (and the doer) doesn’t intend except for the people to see (his actions) for some worldly objective, similar to the munafiqeen (hypocrites) while they pray, as Allah says, “And when they stand for prayer, they stand lazily, showing [themselves to] the people and not remembering Allah except a little”(Surah al-Nisa: 142) .
And similarly Allah ﷻ also described the kuffar (disbelievers) with riyaa (showing off) in His saying, “And do not be like those who came forth from their homes insolently and to be seen by people and avert [them] from the way of Allah.”(Surah Al-Anfal: 47)
And this complete insincerity is rarely found in a mu’min (believer) in his obligatory salah (prayer) or his siyam (fasting). However, it may emanate (in deeds such as) obligatory charity, hajj and other similar actions which are apparent or in those (deeds) whose benefit extends to others, for sincerity in these cases is hard (to maintain). And no muslim doubts that this act (of absolute insincerity) is nullified and that the one who does it is deserving of Allah’s anger and punishment.
The second type
And sometimes the deed is done for Allah ﷻ but is accompanied by riyaa (insincerity). If riyaa (insincerity) is present from the beginning of the deed, then the established textual evidences point to its invalidity and nullification as well.
As it comes in saheeh muslim, “Abu Hurairah (May Allah be pleased with him) said: The Messenger of Allah (ﷺ) said, “Almighty Allah says, ‘I am the One Who is most free from want of partners. He who does a thing for the sake of someone else beside Me, I discard him and his polytheism.”
Also, al-nasai recorded with a good chain that It was narrated that, Abu ‘Umamah Al-Bahili (may Allah be pleased with him) said: “A man came to the Prophet (ﷺ) and said: ‘What do you think of a man who fights seeking reward and fame – what will he have?’ The Messenger of Allah (ﷺ) said: ‘He will not have anything.’ He repeated it three times, and the Prophet (ﷺ) said to him: ‘He will not have anything.’ Then he said: ‘Allah does not accept any deed, except that which is purely for Him, and seeking His Face.'”
Ibn Rajab (rahimahullah) then says, “And we do not know of any difference in this matter among the salaf (the early generations) and it was only the later generations who differed in this issue (i.e. differed regarding the person who mixes sincerity and insincerity from the beginning of the deed).”
The third type
If, for example, a person combines his niyyah (sincere intention) of jihad with another other than riyaa (showing off) such as (an intention to) take a remuneration for some services (rendered), or intending to take something from the war booty, or intending trade, then the reward of the jihad is reduced and their action won’t be invalidated completely.
As it is in saheeh muslim, ‘Abdullah b. ‘Amr that the Messenger of Allah (ﷺ) said: A troop of soldiers who fight in the way of Allah and get their share of the booty receive in advance two-thirds of their reward in the Hereafter and only one-third will remain (to their credit). If they do not receive any booty, they will get their full reward.
As for the ahadith that show that the one who desires by his jihad some purpose of this dunya will have no reward, is in context taken to mean the one who has no objective from jihad except to seek something from the dunya.
The fourth type
As for the deed that is done solely for the sake of Allah ﷻ originally but then insincerity emerges, if it was a thought and the doer pushed it away then this does not harm his deed in any way, none differed regarding this.
If however he continues in this manner and doesn’t push the insincere thoughts away then the question arises does it cause the invalidity of his actions or it doesn’t affect his action at all and he is rewarded for his original intention? The scholars of the salaf differed in this issue. Imaam Ahmad and ibn Jarir al-Tabari preferred the view that his deed isn’t negated by him continuing with insincere thoughts and that he will be rewarded for his original intention. And the same was reported from Al-hasan al-Basri and others.
Ibn Jarir held the view that the difference (among the scholars) is only with regards to those deeds whose beginnings and endings are connected to each other such as salah, fasting and hajj. As for those deeds that aren’t connected in such a manner like the recitation of the Qur’an, dhikr, spending of wealth and spreading of knowledge then insincere thoughts will affect the deed conversely and the intention will have to be renewed.”
Being praised on doing a good deed
Imaam ibn Rajab (rahimahullah) after mentioning the above mentioned ways in which insincerity can affect an act, mentions the case of one who is praised for his righteous deeds and whether it will have any effect on the deed.
He (rahimahullah) says, “As for one who does a deed sincerely for the sake of Allah and Allah causes it to be praised in a good and beautiful manner by the believers and so the doer becomes happy and rejoices at the bounty of Allah and at His mercy and considers it as a glad tiding then this doesn’t affect the deed in any way.
And this is the meaning that is mentioned in the hadith of Abu Dharr (may Allah be pleased with him), The Messenger of Allah (ﷺ) was asked: “Tell us about a person who does some good deed and people praise him, will this be considered as showing off?” He replied, “This is the glad tidings which a believer receives (in this life).” (muslim)
(taken from the book, “Jami’ al-uloom wa al-hikam” by Imaam Ibn Rajab rahimahullah)