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

137
Views
How to toggle item by id or index ? React.js

I need to opet child component by clicked item. FIrst check code:

    <div className="d-flex">
      {boardList.map((list) => (
        <div className="card m-3 p-3" key={list.id}>
          <div className="d-flex flex-column">
            <h6> {list.name} </h6>

            <ul className="list-group">
              {list.cards.map((card) => (
                <li className="list-group-item" key={card.id}>
                  {card.name}
                </li>
              ))}
            </ul> 

            {isVisible ? (
              <TodoForm onCloseForm={onCloseForm} />
            ) : (
              <small 
                className="mt-2"
                onClick={showInput}
              > 
                Add new task + 
              </small>
            )}
          </div>
        </div>
      ))}
    </div>

This is work but when I click on 'Add new task +' a child component opens up to me everywhere. i want only the component with the selected id or index to open.

also component for this :

  const [isVisible, setIsVisible] = useState(false);
  const [boardList, setBoardList] = useState([]);

  useEffect(() => {
    axiosInstance
      .get("")
      .then((res) => {
        setBoardList(res.data);
        console.log("resp", boardList);
      })
      .catch((err) => {
        console.log(err);
      });
  }, []);

  const showInput = () => {
    setIsVisible(true);
  };

  const onCloseForm = () => {
    setIsVisible(false);
  };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

All the items of the resultant array from boardList.map are depending on the same state isVisible, that's why when you click on one of them all the items mimic the same behaviour.

What you need is to create a component with its own state to encapsulate this part of your code

{isVisible ? (
  <TodoForm onCloseForm={onCloseForm} />
  ) : (
  <small 
    className="mt-2"
    onClick={showInput}
  > 
  Add new task + 
</small>
)}

This way every instance of this new component would have its own isVisible so they no longer would affect their siblings state.

The component would look like this.

const NewComponent = () => {
  const [isVisible, setIsVisible] = useState(false);

  return <>
    {isVisible ? (
        <TodoForm onCloseForm={onCloseForm} />
    ) : (
      <small className="mt-2" onClick={() => setIsVisible(true)}>
        Add new task +
      </small>
    )}
  </>
};
about 4 years ago · Juan Pablo Isaza 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!