简体   繁体   English

我在 JavaScript/ReactJS 中遇到问题

[英]I am facing a problem in JavaScript/ReactJS

I am working in ReactJS on post API.我正在 ReactJS 中处理 post API。 I have given a task to create some text fields in which I have to give some values and then on click of a submit button the information from text field will be submitted to POST API which I have taken from JasonPlaceHolder.我给了一个任务来创建一些文本字段,我必须在其中提供一些值,然后单击提交按钮,文本字段中的信息将提交到我从 JasonPlaceHolder 获取的 POST API。

I have written a code which is given at last.我写了一个代码,最后给出了。 Whenever I click Register button the value in last input field (which in my case is "EMAIL") overrides the values of all the input fields.每当我单击“注册”按钮时,最后一个输入字段(在我的情况下为“EMAIL”)中的值会覆盖所有输入字段的值。

A screenshot of my problem is also attached:还附上了我的问题的截图:

问题截图

Code代码

import React from 'react';
import logo from './logo.svg';
import './App.css';
import axios from 'axios';


class App extends React.Component {

  constructor(props){
    super(props);
    this.state = {
          error: null,
          isLoaded: false,
          items: [],
          inter:0,
          ID: '',
          Name: '',
          username : '',
          Email: ''
        };
this.updateInput = this.updateInput.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
  }
  updateInput(event){
    this.setState({ID : event.target.value})
    this.setState({Name : event.target.value})
    this.setState({username : event.target.value})
    this.setState({Email : event.target.value}) 
    }
  handleSubmit(){
    var dict = { id: this.state.ID, name:this.state.Name , username: this.state.username , email: this.state.Email };

    axios.post('https://jsonplaceholder.typicode.com/users', dict)
    .then(response => {
      console.log(response)
      this.setState({isLoaded:true,
                      flag:1,
                    })
    })
    .catch(error => {
      alert(error);
      console.log(error);
    })
    }

  render() {

    const { error, isLoaded, items } = this.state;

    if (this.state.inter == 0){

      return(
      <div>

<form>    
    <p>ID:<input type="text" name="ID" onChange={this.updateInput}/> </p>
    <p>Name:<input type="text" name="name" onChange={this.updateInput} /> </p>
    <p>User Name:<input type="text" name="username" onChange={this.updateInput} /></p>
    <p>Email: <input type="text" name="email" onChange={this.updateInput} /></p> 
</form> 


  
      <button onClick={this.handleSubmit}>
      Register
      </button>

      </div>
      );
 

    }else{

      return(

      <div>
      <button onClick={this.handleSubmit} >
      Register
      </button>

      <h1> Post Request Submitted Successfully</h1>


      </div>

      );
    }

  }

  componentDidMount() {

    }
}

export default App;
    

You have to update the fields like this,你必须像这样更新字段,

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

Change you updateInput logic to this.将您的 updateInput 逻辑更改为此。

updateInput(event){
  this.setState({[event.target.name] : event.target.value})
}

The issue is in the updateInput method.问题出在 updateInput 方法中。 change it to.将其更改为。

const { name, value } = event.target;
this.setState(state => ({
  ...state, [name]: value,
}));

N:B You will have to make the names of the input element same as the state field. N:B 您必须使输入元素的名称与状态字段相同。

Your updateInput method overrides all the other fields, that's why the last updated field (the email) is the one you are seeing on the request payload.您的 updateInput 方法会覆盖所有其他字段,这就是为什么最后更新的字段(电子邮件)是您在请求负载中看到的字段。 To solve this you can separate the updateInput method for each input field or inside updateInput check which field you're currently updating and update only that field.为了解决这个问题,您可以为每个输入字段分离 updateInput 方法,或者在 updateInput 内部检查您当前正在更新的字段并仅更新该字段。

You can check here for more details:您可以在此处查看更多详细信息:

https://medium.com/@zacjones/handle-multiple-inputs-in-react-with-es6-computed-property-name-e3d41861ae46 https://medium.com/@zacjones/handle-multiple-inputs-in-react-with-es6-computed-property-name-e3d41861ae46

I am working in ReactJS on post API.我正在使用React API上的post API。 I have given a task to create some text fields in which I have to give some values and then on click of a submit button the information from text field will be submitted to POST API which I have taken from JasonPlaceHolder.我已经完成了创建一些文本字段的任务,在其中必须提供一些值,然后单击提交按钮,来自文本字段的信息将被提交到我从JasonPlaceHolder那里获取的POST API。

I have written a code which is given at last.我写了最后给出的代码。 Whenever I click Register button the value in last input field (which in my case is "EMAIL") overrides the values of all the input fields.每当我单击“注册”按钮时,最后一个输入字段(在我的情况下为“ EMAIL”)中的值都会覆盖所有输入字段的值。

A screenshot of my problem is also attached:还附上我的问题的屏幕截图:

问题的屏幕截图

Code代码

import React from 'react';
import logo from './logo.svg';
import './App.css';
import axios from 'axios';


class App extends React.Component {

  constructor(props){
    super(props);
    this.state = {
          error: null,
          isLoaded: false,
          items: [],
          inter:0,
          ID: '',
          Name: '',
          username : '',
          Email: ''
        };
this.updateInput = this.updateInput.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
  }
  updateInput(event){
    this.setState({ID : event.target.value})
    this.setState({Name : event.target.value})
    this.setState({username : event.target.value})
    this.setState({Email : event.target.value}) 
    }
  handleSubmit(){
    var dict = { id: this.state.ID, name:this.state.Name , username: this.state.username , email: this.state.Email };

    axios.post('https://jsonplaceholder.typicode.com/users', dict)
    .then(response => {
      console.log(response)
      this.setState({isLoaded:true,
                      flag:1,
                    })
    })
    .catch(error => {
      alert(error);
      console.log(error);
    })
    }

  render() {

    const { error, isLoaded, items } = this.state;

    if (this.state.inter == 0){

      return(
      <div>

<form>    
    <p>ID:<input type="text" name="ID" onChange={this.updateInput}/> </p>
    <p>Name:<input type="text" name="name" onChange={this.updateInput} /> </p>
    <p>User Name:<input type="text" name="username" onChange={this.updateInput} /></p>
    <p>Email: <input type="text" name="email" onChange={this.updateInput} /></p> 
</form> 


  
      <button onClick={this.handleSubmit}>
      Register
      </button>

      </div>
      );
 

    }else{

      return(

      <div>
      <button onClick={this.handleSubmit} >
      Register
      </button>

      <h1> Post Request Submitted Successfully</h1>


      </div>

      );
    }

  }

  componentDidMount() {

    }
}

export default App;
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM