Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

modal for all items shown in one click ( Reactjs )

I have a todo-list app and I want to show more details of a task when its clicked so I’m using the react-modal package and when I click on one item, all of the items modal will pop up and not the single one I clicked on

import { useContext, useState } from "react";

import Task from "../task/task";
import { todosContext } from "../../context/todosContext";
import { motion, AnimatePresence } from "framer-motion";
import Modal from "react-modal";

const Search = (props) => {
  const [query, setQuery] = useState("");
  const { todos, setTodos } = useContext(todosContext);
  const [modalIsOpen, setIsOpen] = useState(false);
  const [openedId, setOpenedId] = useState("");

  const PER_PAGE = 4;
  Modal.setAppElement(document.getElementById("root"));

  function openModal() {
    setIsOpen(true);
  }
  function closeModal() {
    setIsOpen(false);
  }


  return (
    <StyledDiv>
     
      <AnimatePresence>
        {currentPageData.map((todo) => (
          <motion.div
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{
              opacity: 0,
              position: "absolute",
              y: -1000,
              transition: "1s",
            }}
            key={todo.id}
          >
            <Task
              onRequestOpen={openModal}
              key={todo.id}
              title={todo.title}
              description={todo.description}
              backgroundColor="#ffffff"
              disabled={loading}
              onClick={() => deleteHandler(todo.id)}
              placeholder={"completed"}
              onChange={(e) => processChange(e, todo.id)}
              checked={todo.completed}
              id={todo.id}
              htmlFor={todo.id}
              marginTop="-5px"
            ></Task>

            <Modal
              isOpen={modalIsOpen}
              onRequestClose={closeModal}
              style={customStyles}
              contentLabel="Example Modal"
            >
              <button onClick={closeModal}>close</button>
              {todo.description}
            </Modal>
          </motion.div>
        ))}
      </AnimatePresence>
  
    </StyledDiv>
  );
};

export default Search;

the solution I have myself is to make a state called opendItemId and pass the id of the item to it when the user clicks on the item
it works and shows the single and specific detail of the item I clicked on but I can’t close the modal because isOpen which is a property of </Modal component as you can see will be setting the todo.id to opnedItemId state like this isOpen={modalIsOpen(openedId === todo.id)}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First of all, you are creating N modals, with N being the number of the todo items.

So Modal should be outside of your map function. Then you can set the currently selected todo item in your state and load it in your modal.

const [currentTodo, setCurrentTodo] = useState(null);

  const openModal = todoItem => {
    setCurrentTodo(todoItem);
    setIsOpen(true);
  }

.
.
.

        <Task
          onRequestOpen={() => openModal(todo)}
          key={todo.id}

.
.
.

        <Modal
          isOpen={modalIsOpen}
          onRequestClose={closeModal}
          style={customStyles}
          contentLabel="Example Modal"
        >
          <button onClick={closeModal}>close</button>
          {currentToDo.description}
        </Modal>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading