A
A
aznhautroalvyl2018-09-18 17:16:34
JavaScript
aznhautroalvyl, 2018-09-18 17:16:34

Create an object from the entered data in the input?

There are three inputs. When the button is clicked, the values ​​of the inputs are stored in the view object: The object itself is stored in the main component in this.state. After that, the object is passed to the addToDatabase function. Can you tell me how to implement this correctly? I had the following try: mainPage.jsx
{surname: 'Фамилия', name: 'Имя', city: 'Город'}.

class MainPage extends Component {
  constructor(props) {
    super(props);
    this.state = {         
      newPerson: {
        name: '',
        surname: '',
        city: ''
      }
    };
    this.getInputNameValue = this.getInputNameValue.bind(this);
    this.getInputSurnameValue = this.getInputSurnameValue.bind(this);
    this.getInputCityValue = this.getInputCityValue.bind(this);
    this.addToDatabase = this.addToDatabase.bind(this);
  }

  getInputNameValue(inputValue) {
    this.setState({ newPerson.name: inputValue });   // здесь ошибка в использовании newPerson.name
  }

  getInputSurnameValue(inputValue) {
    this.setState({ newPerson.surname: inputValue });   
  }

  getInputCityValue(inputValue) {
    this.setState({ newPerson.city: inputValue });   
  }

  addToDatabase() {
    fetch(`http://127.0.0.1:3000/add/${this.state.newPerson}`, {
      method: 'PUT'
    })
      <...>
  }

  render() {
    return (
      <div>      
        <InputForm getInputNameValue={this.getInputNameValue}
           getInputSurnameValue={this.getInputSurnameValue} getInputCityValue={this.getInputCityValue}               
        />          
        <Footer addToDatabase={this.addToDatabase}/>
      </div>
    );
  }
}

InputForm.jsx
const propTypes = {
  getInputNameValue: PropTypes.func,
  getInputSurnameValue: PropTypes.func,
  getInputCityValue: PropTypes.func  
};

class InputForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      nameP: '',
      surnameP: '',
      cityP: '',
      value: ''
    };
    this.handleNameChange = this.handleNameChange.bind(this);
    this.handleSurnameChange = this.handleSurnameChange.bind(this);
    this.handleCityChange = this.handleCityChange.bind(this);    
  }

  handleNameChange(event) {
    this.setState({ nameP: event.target.value });
    this.props.getInputNameValue(event.target.value);
  }

  handleSurnameChange(event) {
    this.setState({ surnameP: event.target.value });
    this.props.getInputSurnameValue(event.target.value);
  }

  handleCityChange(event) {
    this.setState({ cityP: event.target.value });
    this.props.getInputCityValue(event.target.value);
  }

  render() {
    return (
      <form className='row form'>        
            <label className='col-4 form__label' htmlFor='surnameMembers'>Фамилия</label>
            <input className='col-6 form-control' id='surnameMembers' name='surnameMembers'
              type='text' required value={this.state.surnameP}
              onChange={this.handleSurnameChange}
            />          
            <label className='col-4 form__label' htmlFor='nameMembers'>Имя</label>
            <input className='col-6 form-control' id='nameMembers' name='nameMembers'
              type='text' required value={this.state.nameP}
              onChange={this.handleNameChange}
            />
            <label className='col-4 form__label' htmlFor='cityMembers'>Город</label>
            <input className='col-6 form-control' id='cityMembers' name='cityMembers'
              type='text' required value={this.state.cityP}
              onChange={this.handleCityChange}
            />        
      </form>
    );
  }
}

footer.jsx
const propTypes = {  
  addToDatabase: PropTypes.func
};

class Footer extends Component {
  constructor(props) {
    super(props);
    this.handleSave = this.handleSave.bind(this);   
  }

  handleSave(e) {
    e.preventDefault();
    this.props.addToDatabase();
  }

  render() {
    return (
      <footer className='footer'>
        <div className='container-fluid'>         
                <a className='btn btn-raised btn-warning btn_icon-action btn_icon-action_save' href='#' role='button'
                  onClick={this.handleSave}
                >
                  Сохранить
                </a>     
        </div>
      </footer>
    );
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Alexandrovich, 2018-09-18
@aznhautroalvyl

class MainPage extends Component {
  constructor(props) {
    super(props);
    this.state = {         
      newPerson: {
      }
    };
    this.getInputNameValue = this.getInputNameValue.bind(this);
    this.getInputSurnameValue = this.getInputSurnameValue.bind(this);
    this.getInputCityValue = this.getInputCityValue.bind(this);
    this.addToDatabase = this.addToDatabase.bind(this);
  }

  setNewPerson=(user)=>{
this.setState({
newPerson:user
})
}

  addToDatabase() {
    fetch(`http://127.0.0.1:3000/add/${this.state.newPerson}`, {
      method: 'PUT'
    })
      <...>
  }

  render() {
    return (
      <div>      
        <InputForm setNewPerson={this.setNewPerson} />          
        <Footer addToDatabase={this.addToDatabase}/>
      </div>
    );
  }
}
class InputForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      user:{
      nameP: '',
      surnameP: '',
      cityP: '',
      value: ''
}
    };
    this.handleNameChange = this.handleNameChange.bind(this);
    this.handleSurnameChange = this.handleSurnameChange.bind(this);
    this.handleCityChange = this.handleCityChange.bind(this);    
  }

  handleNameChange(event) {
let user = this.state.user
let newUser = Object.assign(user,{nameP:event.target.value})
    this.setState({ user: newUser });
    this.props.setNewPerson(this.state.user);
  }

  handleSurnameChange(event) {
let user = this.state.user
let newUser = Object.assign(user,{surnameP:e.target.value})
    this.setState({ user: newUser });
   this.props.setNewPerson(this.state.user)
  }

  handleCityChange(event) {
let user = this.state.user
let newUser = Object.assign(user,{cityP:e.target.value})
    this.setState({ user: newUser });
    this.props.setNewPerson(this.state.user)
  }

  render() {
    return (
      <form className='row form'>        
            <label className='col-4 form__label' htmlFor='surnameMembers'>Фамилия</label>
            <input className='col-6 form-control' id='surnameMembers' name='surnameMembers'
              type='text' required value={this.state.surnameP}
              onChange={this.handleSurnameChange}
            />          
            <label className='col-4 form__label' htmlFor='nameMembers'>Имя</label>
            <input className='col-6 form-control' id='nameMembers' name='nameMembers'
              type='text' required value={this.state.nameP}
              onChange={this.handleNameChange}
            />
            <label className='col-4 form__label' htmlFor='cityMembers'>Город</label>
            <input className='col-6 form-control' id='cityMembers' name='cityMembers'
              type='text' required value={this.state.cityP}
              onChange={this.handleCityChange}
            />        
      </form>
    );
  }
}

R
real2210, 2018-09-19
@real2210

Store the value of inputs in your states with this method. (approx. from the official manual)

handleInputChange(event) {
    const target = event.target;
    const name = target.name;

    this.setState({
      [name]: value
    });
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question