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

React text input inside modal not working

i have this custom modal that render a children element

function MyModal({
  children,
  setShow,
}: {
  children: JSX.Element;
  setShow: (data: boolean) => void;
}) {
  return (
    <div
      className="absolute top-0 w-full h-screen flex flex-col justify-center backdrop-blur-md"
      onClick={() => setShow(false)}
    >
      {children}
    </div>
  );
}

export default MyModal;

I wanna render different elements inside of it, so i created this state

 const [element, setElement] = useState<JSX.Element | null>(null);

I’m calling the modal as

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

{show && <MyModal children={element} setShow={setShow} />}

I’m setting the children as this

<button
          onClick={() => {
            setElement(AddNewTask);
            setShow(true);
          }}
        >
          Add
        </button>

And it’s working ok, it opens and closes fine, but the text input of the rendered element is not working, nothing types. This is the AddNewTask component that i’m rendering

const AddNewTask = (): JSX.Element => {
  return (
    <div
      className="bg-green-400 p-20 rounded-md"
      onClick={(e) => e.stopPropagation()}
    >
      <form
        onSubmit={(e) => {
          e.preventDefault();
          addTask();
        }}
      >
        <input
          value={content}
          onChange={(e) => setContent(e.currentTarget.value)}
          placeholder="Tarefa"
        />
      </form>
      <button onClick={addTask}>Adicionar</button>
      <button onClick={() => setShow(false)}>Cancelar</button>
    </div>
  );
};

>Solution :

The setContent method in the AddTask Component is undefined/ only defined through scope. You need to pass it as a prop to the AddTask component to work or alternatively create the state inside the component with a useState hook.

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