Sometimes we have to deal with bad behavior and harassment in our online communities or in real life. It can be a challenge to do that when they provoke you and are trying to upset you on purpose.
For me, what I have found is that taking a breath, calming down, and taking the high road is usually the most effective way to deal with trolls and bad actors. How I deal with it depends on the situation, but basically I try to hold myself to a higher standard than they are currently demonstrating.
In many situations, they assume you are going to be a jerk or hostile based on some assumption or stereotype. When you don't act that way, it often disarms and confuses them. These are the people who now have to rethink their assumptions since you challenged their preconceived notions. These are the people who typically apologize and change their minds when they realize they were the jerks in the situation, not you. They may even become an ally.
And if they are trying to provoke you, they win if you let them provoke you. They are trying to make you look bad by getting you to do something. Then they point at that and use that against you. So, you instead be nice and make it look like they are harassing an innocent victim (which they are). It backfires on them. These are the ones who get pissed off. They were trying to make you look bad, but you flipped it and made them look bad instead.
And sometimes you have to call people out, publicly or privately. Instead of attacking them personally, you call out specific behaviors and tell them that it is not appropriate here, and that they may be asked to leave (or in the case of a community, banned). No name calling. No personal attacks. No gross generalizations. No negative stereotypes. Just "your behavior is inappropriate and you will comply or there will be consequences."
It is hard for them to argue with you when you don't make it personal, especially when you can point to community rules that they agreed to when they joined. And, in the end, they will often respect you even if they disagree with you because you did not make it personal. Plus when it becomes personal, that is when people get really nasty. And that can become a headache really fast. Avoiding making it personal deescalates the situation, whereas making it personal escalates the situation.
There are many tactics for dealing with harassment and misbehavior. Holding yourself to a higher standard than they do is an effective way to beat them. And the fact that they wind up looking bad is a good deterrent for others.
Remember, some of these people are trying to trip you up on purpose. Don't fall for it.