How to Add and Remove Checkbox Values to an Array in React

1 year ago admin Reactjs

In this lesson, we are going to see how to add and remove checkbox values to an array in react js, we are going to use react hooks to do that.


Create the component

First, let's create the component and display the checkboxes

                                                    
                                                                                                                
import React from 'react';

export default function FilterChecks() {
    const [filterParams, setFilterParams] = useState([]);

    const filterCheckboxs = [
        {
            id: 'check1',
            label: 'Hot deal',
            value: 'special_deals'
        },
        {
            id: 'check2',
            label: 'Special offer',
            value: 'special_offer'
        },
        {
            id: 'check3',
            label: 'New',
            value: 'new'
        },
        {
            id: 'check4',
            label: 'Best seller',
            value: 'featured'
        }
    ];

    return (
        <ul className="list-group">
            {
                filterCheckboxs.map(filter => (
                    <li key={filter.id} className="list-group-item">
                        <div className="form-check flex-grow-1">
                            <input 
                                className="form-check-input" 
                                type="checkbox" 
                                value={filter.value} 
                                onChange={(e) => console.log(e)} 
                                id={filter.id} 
                                checked={filterParams.params.find(item => item === filter.value)} />
                            <label className="form-check-label" htmlFor={filter.id}>
                                { filter.label }
                            </label>
                        </div>
                    </li>
                ))
            }
        </ul>
    )
}


Add and remove checkbox values to the array

Next, we add the function to add and remove the values to the array.

                                                        
                                                                                                                        
import React from 'react';

export default function FilterChecks() {
    const [filterParams, setFilterParams] = useState([]);

    const handleInputChange = (e) => {
        let exists = filterParams.find(filter => filter === e.target.value);
        if(exists) {
            const updatedFilters = filterParams.filter(filter => filter !== e.target.value);
            setFilterParams(updatedFilters);
        }else {
            setFilterParams([...filterParams.params, e.target.value])
        }
    }

    const filterCheckboxs = [
        {
            id: 'check1',
            label: 'Hot deal',
            value: 'special_deals'
        },
        {
            id: 'check2',
            label: 'Special offer',
            value: 'special_offer'
        },
        {
            id: 'check3',
            label: 'New',
            value: 'new'
        },
        {
            id: 'check4',
            label: 'Best seller',
            value: 'featured'
        }
    ];

    return (
        <ul className="list-group">
            {
                filterCheckboxs.map(filter => (
                    <li key={filter.id} className="list-group-item">
                        <div className="form-check flex-grow-1">
                            <input 
                                className="form-check-input" 
                                type="checkbox" 
                                value={filter.value} 
                                onChange={(e) => handleInputChange(e)} 
                                id={filter.id} 
                                checked={filterParams.params.find(item => item === filter.value)} />
                            <label className="form-check-label" htmlFor={filter.id}>
                                { filter.label }
                            </label>
                        </div>
                    </li>
                ))
            }
        </ul>
    )
}

Related Tuorials

How to Add Bootstrap 5 Icons in React

In this lesson, we will see how to add Bootstrap 5 Icons in React, we'll walk through the steps to a...


How to Add Bootstrap 5 in React

In this lesson, we will see how to add Bootstrap 5 in React, we'll walk through the steps to add Boo...


How to Access Images from the Assets folder in React

In this lesson, we will see how to access images from the assets folder in React. When working...


How to Listen to a Specific Word in React

In this lesson, we will see how to listen to a specific word in React. Sometimes, when working...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 5

In the last part of this tutorial, we will display the cart items, add the ability to increment/decr...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 4

In the fourth part of this tutorial, we will fetch and display all the products on the home page, an...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 3

In the third part of this tutorial, we will start coding the front end, first, we will install the p...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 2

In the second part of this tutorial, we will create the product and payment controllers, and later w...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 1

In this tutorial, we will create a shopping cart using React js Laravel 11 and Stripe payment gatewa...


How to Use Rich Text Editor in React js

In this lesson, we will see how to use rich text editor in React JS, let's assume that we have a com...