Profanity Filter in React Native
In this post, we will see an interesting library that helps you to create a 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 these kinds of words in chat so I took the help of the Profanity filter.
Profanity Filter helps you to remove the bad words, it captures the bad words from the sentence and replaces them 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 a Profanity Filter to clean the bad words.
Creating a Profanity Filter in React Native is very easy you just have to use bad-words
library and it directly provides a 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 the filter imported you can use the clean function to filter the bad words.
import Filter from 'bad-words';
const filter = new Filter();
filter.clean(inputValue)
Placeholder Overrides
If you want to use your own placeholder then you can use a placeholder while initializing 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 to the blacklist you can pass multiple words in addWords function. Please use the spread operator if you want to use an 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.addWords(...newBadWords);
filter.clean("hello bad1 bad2 bad3!")
//hello **** **** ****!
//or
const filter = new Filter({ list: ['bad1', 'bad2', 'bad3'] });
filter.clean("hello bad1 bad2 bad3!")
//hello **** **** ****!
Instantiate with an Empty List
You can clean the 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 the 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.removeWords(...removeWords);
filter.clean("hello bad1 bad2 bad3!");
//hello bad1 bad2 bad3!
Profanity Filter Example Description
In this example, we will create a simple input that will allow a user to input some sentences which will be filtered using a profanity filter, if our filter found any bad word in a sentence then it will replace it with * (You can also customize * with whatever you want).
So Let’s start with an 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 command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
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 Profanity Filter in React Native
Open App.js in any code editor and replace the code with the following code
App.js
// Profinity Filter in React Native - To Remove bad words
// https://aboutreact.com/react-native-profanity-filter/
// Import React
import React, {useState} from 'react';
// Import all the required components
import {
TextInput,
SafeAreaView,
View,
StyleSheet,
Text
} from 'react-native';
// Import profanity filter
import Filter from 'bad-words';
const App = () => {
let [inputValue, setInputValue] = useState('');
const filter = new Filter();
const handleInput = (value) => {
setInputValue(value);
};
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<Text style={styles.heading}>
Profinity Filter in React Native
{'\n'}
To Remove Bad Words
</Text>
<Text style={styles.insertedTextStyle}>
{inputValue ? filter.clean(inputValue) : ''}
</Text>
<TextInput
value={inputValue}
onChangeText={handleInput}
placeholder={'Pleas Enter any Value'}
style={styles.inputStyle}
/>
<Text style={styles.textStyle}>
Please insert any string with bad word in input {' '}
and you will see it filtered out
</Text>
</View>
</SafeAreaView>
);
};
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
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
Output in Online Emulator
This is how you can make a Profanity Filter in React Native – To Remove bad words. If you have any doubts or 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. 🙂