1

I have an onClick in map function, if I click onClick it will change state of all map item not that item which I clicked. I am using useState hook.

const [open, setOpen] = useState(true);

{
  FilterAvailable.map((item, id) => {
    return (
      <li className="filterItem">
        <div
          className="filterBtn flex align-center justify-between"
          onClick={() => setOpen(!open)}
        >
          <h2>{item.catogery}</h2>
          <div>
            {open ? (
              <IoIosArrowDown className="downArrow" />
            ) : (
              <IoIosArrowUp className="downArrow" />
            )}
          </div>
        </div>

        {item.options.map(option => {
          return (
            <>
              {open && (
                <ul className="filterOption">
                  <li className="filterOptionitem">
                    <button>{option}</button>
                  </li>
                </ul>
              )}
              {/* <div className="hrLine"></div> */}
            </>
          );
        })}
      </li>
    );
  });
}

I want to open only those item option which click.

1
  • You have your onClick function on a button outside the map function. If you want to open an individual item, you must include that onClick inside the button inside the map as well as the index for that item, so if the index of that item matches, it will only open that only. You can have the index as a second argument in the .map function Commented Aug 31, 2021 at 20:35

3 Answers 3

1

Your state is in the wrong place, you need to break the component and have a open state per filterableItem


const Filters = () => {
  return FilterAvailable.map((item, id) => {
    return <FilterItem item={item} />;
  });
};

const FilterItem = ({ item }) => {
  const [open, setOpen] = useState(true);

  return (
    <li className="filterItem">
      <div
        className="filterBtn flex align-center justify-between"
        onClick={() => setOpen(!open)}
      >
        <h2>{item.catogery}</h2>
        <div>
          {open ? (
            <IoIosArrowDown className="downArrow" />
          ) : (
            <IoIosArrowUp className="downArrow" />
          )}
        </div>
      </div>

      {item.options.map(option => {
        return (
          <>
            {open && (
              <ul className="filterOption">
                <li className="filterOptionitem">
                  <button>{option}</button>
                </li>
              </ul>
            )}
          </>
        );
      })}
    </li>
  );
};


Sign up to request clarification or add additional context in comments.

1 Comment

One more problem. I want to get value of option with item category when click on it how can i achive this, kindly guide me.
0

Put the index inside state on onclick , inside map check if state index equals to map index and you are done

1 Comment

Sorry but i didn't get you
0

Consider moving the mapped elements to their own component with props - then you create and set your open and closed state there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.