The command below replaces the player's held item slot with a carrot if it has anything other than a carrot.
/item replace entity @a[nbt=!{SelectedItem:{id:"minecraft:carrot"}}] weapon.mainhand with minecraft:carrot
If this command is put in a repeating command block and you eat a carrot in survival mode it will try replace the item in your hand, but it will appear invisible until you update the slot.
Thank you for your report!
We're tracking this issue in MC-239935, so this ticket is being resolved and linked as a duplicate.
If you would like to add a vote and any extra information to the main ticket it would be appreciated.
If you haven't already, you might like to make use of the search feature to see if the issue has already been mentioned.
Quick Links:
📓 Bug Tracker Guidelines – 💬 Community Support – 📧 Mojang Support (Technical Issues) – 📧 Microsoft Support (Account Issues)
📓 Project Summary – ✍️ Feedback and Suggestions – 📖 Game Wiki