Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to populate with initial values #6

Open
peterjp80 opened this issue Aug 16, 2018 · 1 comment
Open

Unable to populate with initial values #6

peterjp80 opened this issue Aug 16, 2018 · 1 comment

Comments

@peterjp80
Copy link

peterjp80 commented Aug 16, 2018

I have a requirement that the react-multi-email field show some existing email addresses, but I am unable to populate the field with initial values. My container loads the component and shows the field without any javascript errors, and the react-multi-email field shows, but it is empty. Below is the code for the component. I am setting the "emails" attribute to an array with a single value of "[email protected]".

import PropTypes from "prop-types";
import React from "react";
import ReactModal from "react-modal";
import { ReactMultiEmail } from 'react-multi-email';

const TestMultiEmailModal = ({ showModal }) => {
    return (
        <ReactModal contentLabel="Submit Internally" isOpen={showModal} className="react-modal" overlayClassName="react-modal-overlay">
            <ReactMultiEmail
                style={{}}
                emails={["[email protected]"]}
                onChange={_emails => {
                    this.setState({ emails: _emails });
                }}
                getLabel={(
                    email,
                    index,
                    removeEmail,
                ) => {
                    return (
                        <Label key={index}>
                            {email}
                            <Icon name="delete" onClick={() => removeEmail(index)} />
                        </Label>
                    );
                }}
            />
        </ReactModal>
    );
};

TestMultiEmailModal.propTypes = {
    showModal: PropTypes.bool
};

export default TestMultiEmailModal;
@thomasJang
Copy link
Member

The reason why it does not work is that the value of ReactMultiEmail.props.email is not managed by state.
It is recommended to manage the value of emails as state as the following code.
Have a happy coding.

import PropTypes from "prop-types";
import React from "react";
import ReactModal from "react-modal";
import { ReactMultiEmail } from 'react-multi-email';


class TestMultiEmailModal extends React.Component {

  state = {
    emails: ["[email protected]"]
  }

  render(){
    const {showModal} = this.props;
    const {emails} = this.state;

    return (
      <ReactModal contentLabel="Submit Internally" isOpen={showModal} className="react-modal" overlayClassName="react-modal-overlay">
          <ReactMultiEmail
              style={{}}
              emails={emails}
              onChange={_emails => {
                  this.setState({ emails: _emails });
              }}
              getLabel={(
                  email,
                  index,
                  removeEmail,
              ) => {
                  return (
                      <Label key={index}>
                          {email}
                          <Icon name="delete" onClick={() => removeEmail(index)} />
                      </Label>
                  );
              }}
          />
      </ReactModal>
    );
  }
  
}

TestMultiEmailModal.propTypes = {
    showModal: PropTypes.bool
};

export default TestMultiEmailModal;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants