Bad behaviour can, of course, be just that or it can be symptomatic of an underlying mental health issue.
Examples of this can include:
Attention seeking - Trying to gain attention from those you love
Self-medication - Chemical solution to an emotional problem
Compensation - Looking for recognition in the wrong places
If these things are sitting on the surface of an underlying issue, it is essential to recognise that and ask if you really want to teach that person a lesson in the moment or if you would prefer to support them in addressing the issue itself. The latter is what is needed to find a long-term solution.
Though the two things are connected there are in fact, two issues that need addressing here - the root cause and crisis moments - in which the issue boils to the surface in the form of bad behaviour.
Managing a crisis
Next time you are tempted to react to bad behaviour, try to understand that for the person who is going through that experience, a crisis moment is full of confusion, frustration and fear. Because it can be so blinding, at this time, there is no chance of addressing the root cause. In reality, the only thing you can gain here is to dig deep, be sympathetic and do whatever you can to help them calm down.
Try to see that the person you care about is struggling in these moments. They are having an irrational experience, and the only progress that can be made is to play your role in deescalating the situation.
In doing this, you will not only help to settle things down, but you can also help them to see that they no longer need to continue the attention-seeking or to look for their chemical solution. They have the short term support from you that they need.
Addressing root cause
The time for doing this is in a calm moment. When you have time to talk and the person you care about is in a rational state of mind. Funnily enough, here, tough love is more appropriate.
Now is the time that they are more able to listen. You have the opportunity to help them to recognise their issue, listen to their side of the story and let them know that they have your support. You can also encourage them to speak to a professional who can work with them on the journey of addressing the underlying issue.
Credit where credit is due
Not reacting on those moments is incredibly hard. It takes extreme patience and empathy. Not only is this an opportunity for you to practice and advance these skills but by keeping one eye on the long term goal, you will do much more to help the person you care about than you would by punishing them at the time.