Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
Leave nested checkbox click with children and parents in React

I have a function that receives values ​​from a json that contains nested values ​​(parent and children with multiple layers) and this function sets up a checkbox for each item, with its children below its respective parent.

I would like help when clicking on a parent checkbox, all the child checkboxes will be selected, when clicking on a child checkbox and the other children are not clicked, the parent element will have an "indeterminate" state, if there is only one child checkbox and it is clicked or all children are clicked the parent element is selected.

You can see the screenshot of how it should behave by clicking here.

Here you can see the project online.

My code:

CheckboxItem Component:

import { useState } from "react";
import Checkbox from "react-three-state-checkbox";

export function CheckboxItem(props) {
  const [isChecked, setIsChecked] = useState(false);

  function clickCheckbox() {
    setIsChecked(!isChecked);
  }
  return (
    <>
      <Checkbox
        checked={isChecked}
        // indeterminate={props.indeterminate}
        onChange={clickCheckbox}
      />
      {props.children}
    </>
  );
}

ChildrenMap Component:

import { CheckboxItem } from "../CheckboxItem";
import * as S from "./styled";

export function ChildrenMap({ itens }) {
  const filhos = Object.values(itens);
  return (
    <div>
      {filhos.map((item, i) => (
        <S.Itens key={i}>
          <CheckboxItem>
            {item.name}
          </CheckboxItem>
          <ChildrenMap itens={item.children} key={i} />
        </S.Itens>
      ))}
    </div>
  );
}

Main File:

import React from "react";
import ReactDOM from "react-dom";
import { ChildrenMap } from "./components/ChildrenMap";
import data from "./data.json";

function App() {
  return (
    <>
      {data.itens.map((item, i) => (
        <div key={i}>
          <ChildrenMap itens={item} key={i} />
        </div>
      ))}
    </>
  );
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!