react-modal-输入的onChange未更新状态

I hope you will be able to help me with an answer to my question. I am using react-modal and inside the modal I have an input where users can write an email. When writing the email it should update the state but that is not happening.

现在发生的事情是,每当我写一个新字母时,模型都会重新渲染,并且导致状态仅随着我输入的第一个字母而更新,然后失去焦点。

我正在使用反应挂钩。我知道这会有所改变。

我的代码如下所示:

    import React, { useState, useContext } from 'react';
import AppContext from '../../AppContext.jsx';
import GroupContext from './GroupContext.jsx';
import Section from '../../Elements/PageContent/Section.jsx';
import PageTitle from '../../Elements/PageContent/PageTitle.jsx';
import WhiteContainer from '../../Elements/PageContent/WhiteContainer.jsx';
import { Form, FormGroup, FormSection, FormSection_Split, Label, Input, Select, Submit } from '../../Elements/Forms/FormCollection.jsx';
import { MusicGenres } from '../../Elements/Forms/MusicGenres.jsx';
import { Years } from '../../Elements/Forms/Years.jsx';
import { H3 } from '../../Elements/Fonts/FontCollection.jsx';
import { Icon } from '../../Elements/Image/ImageUtil.jsx';
import ReactModal from "react-modal";
import { useModal } from "react-modal-hook";

export default function Groups(props) {  
    const AC = useContext(AppContext);
    const GC = useContext(GroupContext);
    const [groupName, setGroupName] = useState("");
    const [groupDescription, setGroupDescription] = useState("");
    const [memberEmail, setMemberEmail] = useState("");
    const [groupMembers, setGroupMembers] = useState([]);
    const [showModal, hideModal] = useModal(() => (
        <ReactModal className="DialogPopup" isOpen ariaHideApp={false}>
            <Form>
                <FormGroup>
                    <FormSection>
                        <Label htmlFor="memberEmail" title="Email of your group member:" />
                        <Input type="email" name="memberEmail" value={memberEmail} onChange={(e) => setMemberEmail(e.target.value)} placeholder="@" />
                    </FormSection>
                </FormGroup>
            </Form>
            <button onClick={(e) => hideModal()} className="Close" aria-label="Close popup"><Icon iconClass="fal fa-times" /></button>
        </ReactModal>
    ), []);

    async function addObjectToGroupMembersArray(e) {
        e.preventDefault();
        console.log("Adding member");
    }

    return (
        <React.Fragment>
            <PageTitle title="Add group" />
            <Section>
                <Form>
                    <FormGroup>
                        <FormSection>
                            <WhiteContainer>
                                <Label htmlFor="groupName" title="Group name:" />
                                <Input type="text" name="groupName" value={groupName} onChange={(e) => setGroupName(e.target.value)} maxLength="60" required />
                                <span className="CharactersLeft">Characters left: {60 - groupName.length}</span>
                            </WhiteContainer>
                        </FormSection>
                        <FormSection>
                            <WhiteContainer>
                                <Label htmlFor="groupDescription" title="Describe your group:" />
                                <textarea name="groupDescription" id="groupDescription" value={groupDescription} onChange={(e) => setGroupDescription(e.target.value)} maxLength="500"></textarea>
                                <span className="CharactersLeft">Characters left: {500 - groupDescription.length}</span>
                            </WhiteContainer>
                        </FormSection>
                        <FormSection>
                            <WhiteContainer>
                                <Label htmlFor="groupMembers" title="List the emails of your group members?" />
                                <a href="#" className="AddLink" aria-label="Add member" title="Click to add a member" onClick={(e) => { e.preventDefault(); showModal(); }}>
                                    <Icon iconClass="fal fa-plus" />
                                </a>
                            </WhiteContainer>
                        </FormSection>
                        <FormSection className="FormSection--Submit">
                            <Submit text="Create group" />
                        </FormSection>
                    </FormGroup>
                </Form>
            </Section>
        </React.Fragment>
    );
}

你们中有谁知道为什么每次键入时都会更新模态,从而导致无法在输入中写入任何内容。我应该使用“ ref”吗?如果应该,我该怎么做?

The onChange method I am using is always working, just not inside react-modal.

评论
  • miguel
    miguel 回复

    如何创建将在onChange中调用的外部函数?

    就像是:

    
    const handleOnChange = (event) => {
       setMemberEmail(event.target.value);
    }
    
    // then call it in your component
    <Input type="email" name="memberEmail" value={memberEmail} onChange={handleOnChange} placeholder="@" />