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

94
Views
Toggle the button name on click in ReactJS

I have an array of objects. So for every object, which has subItems, I'm trying to add a button to it. On click of the particular button, the name of the particular button should toggle between 'Expand' and 'Hide'. I'm displaying them using map.

export default function App() {

  const [button,setButton] = useState([
    {pin: 'Expand', id: 0},
    {pin: 'Expand', id: 1},
    {pin: 'Expand', id: 2},
  ]);

  const dataObj = [
    {
      title: 'Home'
    },
    {
      title: 'Service',
      subItems: ['cooking','sleeping']
    },
    {
      title: 'Contact',
      subItems: ['phone','mobile']
    }
  ];

  const expandFunc = (ind) => {
   // toggle logic here
  }
  return (
    <div className="App">
      {
        dataObj.map((arr,ind) => (
          <div>
            <span>{arr.title}:</span>
            {
              // console.log(ind)
              arr.subItems && 
              <button onClick={() => expandFunc(ind)}>{button[ind].pin}</button>
            }
          </div>
        ))
      }
    </div>
  );
}

This is how the output looks - enter image description here

If I click on service button, then the button name should toggle between 'expand' and 'hide'. Could someone help me with this?

almost 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You need to update your state by determining new pin based on current state, try using Array.map:

const expandFunc = (ind) => {
    const togglePin = oldPin => oldPin === "Expand" ? "Hide" : "Expand";
    const updatedButtons = button.map((btn, index) => 
        ind === index ? { ...btn, pin: togglePin(btn.pin) } : btn);
    setButton(updatedButtons);
}
almost 4 years ago · Santiago Trujillo Report

0

You can use something like this, I'll also suggest combining dataObj into button state, and using key while mapping elements in React helps to skip them in the rendering process making your site faster.

export default function App() {

  const [button, setButton] = useState([{
      expanded: false,
      id: 0
    },
    {
      expanded: false,
      id: 1
    },
    {
      expanded: false,
      id: 2
    },
  ]);

  const dataObj = [{
      title: 'Home'
    },
    {
      title: 'Service',
      subItems: ['cooking', 'sleeping']
    },
    {
      title: 'Contact',
      subItems: ['phone', 'mobile']
    }
  ];

  const toggleExpandFunc = useCallback((ind) => {
    // toggle logic here
    setButton([...button.map(btn => btn.id === ind ? { ...btn,
      expanded: !btn.expanded
    } : btn)]);
  }, [button]);
  
  return ( <
    div className = "App" > {
      dataObj.map((arr, ind) => ( <
        div >
        <
        span > {
          arr.title
        }: < /span> {
          // console.log(ind)
          arr.subItems &&
            <
            button onClick = {
              () => toggleExpandFunc(ind)
            } > {
              button[ind].expanded ? 'Expanded' : 'Hide'
            } < /button>
        } <
        /div>
      ))
    } <
    /div>
  );
}

almost 4 years ago · Santiago Trujillo Report

0

You can also need another state like Toggle and expnadFunc can be handled like this;

const expandFunc = (ind) => {
    let tmp = [...button];
    tmp.map((arr, index) => {
      if (index === ind) {
        return (arr.pin = arr.pin === 'Toggle' ? 'Expand' : 'Toggle');
      }
      return arr;
    });
    setButton(tmp);
  };
almost 4 years ago · Santiago Trujillo Report
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!