softare development

How to Filter Vulgar Words in React Native

If you’re building a social, chat, or comment-based mobile app using React Native, protecting your users from offensive or vulgar language is crucial for maintaining a safe environment.

In this article, you’ll learn how to filter out vulgar or inappropriate words from user-generated content using simple and effective strategies in React Native.

See How to Toggle Password Visibility in React Native

Why Filter Vulgar Words?

Offensive language can:

  • Harm the user experience.
  • Violate app store policies (Google Play, Apple App Store).
  • Damage your brand’s reputation.
  • Lead to account bans or user complaints.

Tools & Libraries to Use

There are several approaches to profanity filtering, but we’ll explore the most beginner-friendly one using:

  • bad-words: A lightweight JavaScript filter for bad words.
  • A custom word blacklist (optional).

Step 1: Install the bad-words Library

npm install bad-words

or

yarn add bad-words

Step 2: Filter Input Text

Let’s create a function to filter text before it’s submitted or displayed:

import Filter from 'bad-words';

const filter = new Filter();

export const cleanText = (text: string): string => {
  return filter.clean(text);
};

Example Usage in a Chat App

import React, { useState } from 'react';
import { View, TextInput, Button, Text, StyleSheet } from 'react-native';
import { cleanText } from './filterService'; // assume you created this function

export default function MessageInput() {
  const [input, setInput] = useState('');
  const [submitted, setSubmitted] = useState('');

  const handleSend = () => {
    const cleaned = cleanText(input);
    setSubmitted(cleaned);
    setInput('');
  };

  return (
    <View style={styles.container}>
      <TextInput
        style={styles.input}
        value={input}
        onChangeText={setInput}
        placeholder="Type your message..."
      />
      <Button title="Send" onPress={handleSend} />
      <Text style={styles.output}>Filtered: {submitted}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: { padding: 20 },
  input: {
    borderWidth: 1,
    borderColor: '#ccc',
    marginBottom: 10,
    padding: 10,
    fontSize: 16,
  },
  output: {
    marginTop: 10,
    fontStyle: 'italic',
    color: '#333',
  },
});

Conclusion

Filtering vulgar words in React Native is easy using the bad-words package. Whether you’re building a chat app, comments system, or forum, adding a profanity filter helps you keep your app clean and community-friendly.

Recent Posts

How to Build Faster Mobile Apps With Native Wind Library

The Cross-Platform ImperativeLet's face it: building separate iOS and Android apps wastes resources. React Native…

2 days ago

The Surprisingly Simple Secret to Getting Things Done

We live in an age of infinite distraction and overwhelming ambition. Grand goals shimmer on…

3 days ago

How to Create Reusable Components in React JS

Reusable components are modular UI building blocks designed for versatility. Instead of writing duplicate code…

3 days ago

Check if Number, Word or Phrase is a Palindrome in JavaScript

What is a Palindrome? A palindrome is any word, phrase, number, or sequence that reads…

1 week ago

How Facial Recognition Software Works

Facial recognition technology is rapidly changing how we interact with devices, access services, and enhance…

2 weeks ago

Why Grok 4 is the AI Game-Changer You Need to Know

Move over ChatGPT, there's a new, significantly upgraded player causing a stir. xAI, Elon Musk's…

3 weeks ago