Profanity Filter in React Native – To Remove bad words

Profanity Filter in React Native

In this post we will see an interesting library which help you to create Profanity Filter in React Native – To Remove bad words. I was working with a chat app and the requirement was to make a clean social connection with people, here word “clean” means no one should use any offensive language or any word. As a developer you can only instruct your users but can’t stop them to use this kind of words in chat so I took the help of Profanity filter.

Profanity Filter helps you to remove the bad words, it captures the bad words from the sentence and replace it with the placeholder. If you are creating any social media app/chat or anything where a user can share something with others then you can use Profanity Filter to clean the bad words.

Creating Profanity Filter in React Native is very easy you just have to use bad-words library and it directly provides the clean function.

How to Filter Bad Words

To filter bad words you just need to import the dependency and have to create an object of Filter. Once you have filter imported you can use clean function to filter the bad words.

import Filter from 'bad-words';
const filter = new Filter();

Placeholder Overrides

If you want to use your own placeholder then you can use placeholder while initialising Filter.

const Filter = require('bad-words');
let customFilter = new Filter({ placeHolder: 'x'});
customFilter.clean("Don't be an ash0le"); //Don't be an xxxxxx

Add Words to the Blacklist

To add words in the black list you can pass multiple words in addWords function. Please use spread operator if you want to use array.

const filter = new Filter(); 

filter.addWords('bad1', 'bad2', 'bad3');
filter.clean("hello bad1 bad2 bad3!")
//hello **** **** ****!

//or use an array using the spread operator

let newBadWords = ['bad1', 'bad2', 'bad3'];
filter.clean("hello bad1 bad2 bad3!")
//hello **** **** ****!


const filter = new Filter({ list: ['bad1', 'bad2', 'bad3'] }); 
filter.clean("hello bad1 bad2 bad3!")
//hello **** **** ****!

Instantiate with an Empty List

You can clean complete list and can start from initial

const filter = new Filter({ emptyList: true }); 
filter.clean("hell this wont clean anything");
//hell this wont clean anything

Remove Words From the Blacklist

To remove any word from blacklist you can use removeWords function.

const filter = new Filter(); 

filter.removeWords('bad1', 'bad2', 'bad3');
filter.clean("hello bad1 bad2 bad3!");
//hello bad1 bad2 bad3!

//or use an array using the spread operator

let removeWords = ['bad1', 'bad2', 'bad3'];
filter.clean("hello bad1 bad2 bad3!");
//hello bad1 bad2 bad3!

Profanity Filter Example Description

In this example we will create a simple input which will allow a user to input some sentences which will be filtered using profanity filter, if our filter found any bad word in sentence then it will replace it with * (You can also customise * with whatever you want).

So Let’s start with the example.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To use Filter you need to install bad-words dependency and to do that open the terminal and jump into the project using

cd ProjectName

Run the following commands

npm install --save bad-words

This command will copy all the dependencies into your node_module directory. –save is optional, it is just to update the dependency in your package.json file.

Code To Make Profinity Filter in React Native

Open App.js in any code editor and replace the code with the following code


// Profinity Filter in React Native - To Remove bad words

// Import React
import React, {useState} from 'react';

// Import all the required components
import {
} from 'react-native';

// Import profanity filter
import Filter from 'bad-words';

const App = () => {
  let [inputValue, setInputValue] = useState('');

  const filter = new Filter();

  const handleInput = (value) => {

  return (
    <SafeAreaView style={{flex: 1}}>
      <View style={styles.container}>
        <Text style={styles.heading}>
          Profinity Filter in React Native
          To Remove Bad Words
        <Text style={styles.insertedTextStyle}>
          {inputValue ? filter.clean(inputValue) : ''}
          placeholder={'Pleas Enter any Value'}
        <Text style={styles.textStyle}>
          Please insert any string with bad word in input {' '}
          and you will see it filtered out

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: '#ffffff',
    padding: 16,
  heading: {
    fontSize: 22,
    textAlign: 'center',
    marginBottom: 16,
    marginTop: 150,
  textStyle: {
    marginVertical: 10,
    textAlign: 'center',
  insertedTextStyle: {
    fontSize: 16,
    fontWeight: 'bold',
    marginVertical: 20,
    textAlign: 'center',
  inputStyle: {
    width: '100%',
    height: 44,
    padding: 10,
    marginBottom: 10,
    backgroundColor: '#ecf0f1',

export default App;

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

To run the project on an Android Virtual Device or on real debugging device

react-native run-android

or on the iOS Simulator by running (macOS only)

react-native run-ios

Download Source Code

Output Screenshots


Output in Online Emulator

This is how you can make Profanity Filter in React Native – To Remove bad words. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.