React checkbox if checked add value to array


Hi this might be a very newbie question to ask but here is my question. I have check boxes that represent all the days of the week. If a day is checked I want to add it to a array so that on submit I can pass the array to back-end. I copied check box structure from bootstrap to start with. I tried to add logic to see if a box is checked and if it is checked I want to push it to my empty array in state. Also if a checkbox was checked but then unchecked I want to remove the item from the state array. So far I haven’t found a way to make this all work.

import React from 'react'

class CalenderSettingsModal extends React.Component {
    constructor(props) {
            workStart: 8,
            workEnd: '',


        handleCheckboxChange = (event)=> this.setState({workDays:});


    render() {

        return (
                                <h5>Select your workday(s):</h5>
                                <div class="custom-control custom-checkbox " >
                                    <input type="checkbox" class="custom-control-input" id="monday" value="monday"  onChange={this.handleCheckboxChange}/>
                                    <label class="custom-control-label" for="monday">Monday</label>

                                <div class="custom-control custom-checkbox">
                                    <input type="checkbox" class="custom-control-input" id="tuesday" value="tuesday" onChange={ this.handleCheckboxChange}/>
                                    <label class="custom-control-label" for="tuesday">Tuesday</label>


                            <button >Save settings</button>



export default CalenderSettingsModal;

With this so far I tried to accomplish assigning the value to workDays when the box is checked but isn’t working and I’m not sure how I could accomplish what I want to do. Any insight would be appreciated.


Right now, you are assigning the workDays array to a string type.

You can use the spread operator to add values to your workDays state array. If is already present in the workDays state you can filter it. Here is the working code snippet.

Edit reverent-franklin-ziqrn

 handleCheckboxChange = event => {
    let newArray = [...this.state.workDays,];
    if (this.state.workDays.includes( {
      newArray = newArray.filter(day => day !==;
      workDays: newArray

Answered By – Ajin Kabeer

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published