Few different ways you can store data in a React application without allowing duplicates

Sonjoy Chandra Barman
3 min readDec 31, 2022
remove duplicates in javascript

There are a few different ways you can store data in a React application without allowing duplicates. One option is to use a JavaScript Set to store the data. Sets are a collection of unique values, which means that if you try to add a value to a Set that already exists in the Set, the value will not be added again.

Here’s an example of how you could use a Set to store data in a React component:

import React, { useState } from 'react';

function MyComponent() {
const [data, setData] = useState(new Set());
const handleAddData = (value) => {
setData(new Set([...data, value]));
}
return (
<div>
<button onClick={() => handleAddData('value1')}>Add Value 1</button>
<button onClick={() => handleAddData('value2')}>Add Value 2</button>
<button onClick={() => handleAddData('value1')}>Add Value 1</button>
</div>
);
}

In this example, we have a Set called data that is stored in the component's state using the useState hook. We also have three buttons that each call the handleAddData function when clicked. The handleAddData function updates the data Set by adding the value passed to it.

If you click the first “Add Value 1” button, the value “value1” will be added to the data Set. If you click the second "Add Value 2" button, the value "value2" will be added to the data Set. However, if you click the third "Add Value 1" button, the value "value1" will not be added to the data Set again because it is already in the Set.

Another option for storing data without duplicates in a React application is to use an array and check for duplicates before adding a new value to the array. Here’s an example of how you could do this:

import React, { useState } from 'react';

function MyComponent() {
const [data, setData] = useState([]);
const handleAddData = (value) => {
if (!data.includes(value)) {
setData([...data, value]);
}
}
return (
<div>
<button onClick={() => handleAddData('value1')}>Add Value 1</button>
<button onClick={() => handleAddData('value2')}>Add Value 2</button>
<button onClick={() => handleAddData('value1')}>Add Value 1</button>
</div>
);
}

In this example, we have an array called data that is stored in the component's state using the useState hook. We also have three buttons that each call the handleAddData function when clicked. The handleAddData function checks if the value passed to it already exists in the data array using the includes method. If the value does not exist in the array, it is added using the spread operator. If the value does exist in the array, it is not added again.

To remove an element from an array in React, you can use the filter method to create a new array that does not include the element you want to remove. Here's an example of how you could use the filter method to remove an element from an array stored in the component's state:

import React, { useState } from 'react';

function MyComponent() {
const [data, setData] = useState([{ id: 1, value: 'value1' }, { id: 2, value: 'value2' }]);
const handleRemoveData = (id) => {
setData(data.filter((item) => item.id !== id));
}
return (
<div>
{data.map((item) => (
<div key={item.id}>
{item.value}
<button onClick={() => handleRemoveData(item.id)}>Remove</button>
</div>
))}
</div>
);
}

In this example, we have an array of objects called data that is stored in the component's state using the useState hook. Each object in the array has an id and a value property. We also have a handleRemoveData function that is called when the "Remove" button next to each item is clicked.

The handleRemoveData function updates the data array by calling the filter method on the array and passing it a callback function. The callback function receives each item in the array as an argument and returns true if the item should be kept in the array or false if it should be removed. In this case, we are using the filter method to remove the item with the specified id by returning false if the id of the current item matches the id passed to the handleRemoveData function.

--

--

Sonjoy Chandra Barman

I am excited to continue my career as a full-stack developer and am always looking for new challenges and opportunities to grow and learn.