A
A
Artem Andreev2017-01-26 20:43:46
JavaScript
Artem Andreev, 2017-01-26 20:43:46

I can't figure out how to remove the element (Or return to InItialState "If possible")?

With the first click he adds, but with the second click I don’t know how to do it so that he removes the data

var Contact = React.createClass({
    getInitialState: function() {
      return {
      isOpened: {
        address: '' ,
        email: ''
      }
      }
    },
    isOpened: function(event) {

        this.setState({
            isOpened: {
              address: this.props.address ,
              email: this.props.email}
              }
            );
      },
    render:function() {
      return (
        <li className="contact" onClick={this.isOpened}>
          <img className="contact-image" src={this.props.image} width="68px" height="68px"/>
          <div className="contact-info">
            <div className="contact-name" >{this.props.name}</div>
            <div className="contact-number" >{this.props.phoneNumber}</div>
            <div>{this.state.isOpened.address}</div>
            <div>{this.state.isOpened.email}</div>
          </div>
        </li>
      )

    }
  });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Gushchin, 2017-01-26
@Pibodi

Why don't you use classes?

const emptyContacts = {
  address: '' ,
  email: '',
}

var Contact = React.createClass({
    getInitialState: function() {
      return {
       contactData: emptyContacts,
      }
    },
    toggleContactsData: function(event) {
        this.setState({
            contactData: thsi.state.contactsData === emptyContacts
              ? { address: this.props.address, email: this.props.email, }
              : emptyContacts
        });
    },

    render:function() {
      return (
        <li className="contact" onClick={this.toggleContactsData}>
          <img className="contact-image" src={this.props.image} width="68px" height="68px"/>
          <div className="contact-info">
            <div className="contact-name" >{this.props.name}</div>
            <div className="contact-number" >{this.props.phoneNumber}</div>
            <div>{this.state.contactData.address}</div>
            <div>{this.state.contactData.email}</div>
          </div>
        </li>
      )

    }
  });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question