亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

提交按鈕需要點擊 2 次才能生成輸出

提交按鈕需要點擊 2 次才能生成輸出

小唯快跑啊 2022-07-08 15:40:59
我試圖僅在提交表單后更新狀態。我有一個 html 表單,它有 1 個文本輸入和一個提交按鈕,但是需要點擊 2 次提交按鈕才能真正改變反應中的狀態。我正在使用 2 種方法handleSubmit和handleChange.handleChange查找輸入字段的變化并相應地更新狀態。handleSubmithandleChange在表單提交時將更新的狀態附加到數組狀態包含 { itemslist: [], currentitem: "" }當第一次單擊提交按鈕時,它會給出項目的先前值(或給出空數組),而在第二次它給出輸入字段中存在值的數組。下面是我的完整代碼import React from 'react';class App extends React.Component{  constructor(){    super()    this.state = {      currentitem: '',      itemslist: []    }    this.handleChange = this.handleChange.bind(this)    this.handleSubmit = this.handleSubmit.bind(this)  }  handleSubmit(event){    event.preventDefault();    this.setState((prevState) => {      return{        itemslist: prevState.itemslist.concat([this.state.currentitem])      }    })    console.log(this.state.items)  }  handleChange(event){    const {name, value} = event.target    this.setState({ [name] : value })    console.log(this.state.currentitem)  }  render(){    return(      <div>        <form onSubmit={this.handleSubmit} >          <input type='text' placeholder='enter text' name='currentitem' onChange={this.handleChange} value={this.state.currentitem} />          <button type='submit'>Submit</button>        </form>      </div>    )  }}export default App;
查看完整描述

2 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

這個答案可能與您的代碼有點不同,但這樣它就可以工作。將按鈕類型設置為按鈕并使按鈕處理提交,而不是表單。然后將 handleSubmit 函數更改為我所擁有的。我已經嘗試過了,它確實有效?。?/p>


import React from 'react';


class App extends React.Component{

  constructor(){

    super()

    this.state = {

      currentitem: '',

      itemslist: []

    }

    this.handleChange = this.handleChange.bind(this)

    this.handleSubmit = this.handleSubmit.bind(this)

  }



  handleSubmit(e){

    e.preventDefault();

    const { currentitem, itemslist } = this.state;

    const newArray = [

      ...itemslist,

      currentitem

    ];

    this.setState({ itemslist, newArray });

  }


  handleChange(event){

    const {name, value} = event.target

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

    console.log(this.state.currentitem)

  }



  render(){

    return(

      <div>

        <form>

          <input type='text' placeholder='enter text' name='currentitem' onChange={this.handleChange} value={this.state.currentitem} />

          <button type='button' onClick={this.handleSubmit}>Submit</button>

        </form>


        // In cas eyou want to see the values in the array+

        {

            this.state.itemsList.map((item) => <p>{item}</>)

        }

      </div>

    )

  }

}


export default App;


查看完整回答
反對 回復 2022-07-08
?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

setState函數在 react 中是異步的,因此您無法立即獲取更新的值。但是如果你需要從 state 中獲取最近更新的值,你必須使用setState的回調函數。


this.setState({items: newItems}, () => { console.log(); })

我已經修改了您的示例,如下所示以滿足您的要求。


import React from 'react';


class App extends React.Component {

    constructor() {

        super();

        this.state = {

            currentitem: '',

            items: []

        };

        this.handleChange = this.handleChange.bind(this);

        this.handleSubmit = this.handleSubmit.bind(this);

    }


    handleSubmit(event) {

        event.preventDefault();

        this.setState((prevState) => {

            return {

                items: prevState.items.concat([this.state.currentitem])

            }

        }, () => {

            console.log(this.state.items)

        });

    }


    handleChange(event) {

        const {name, value} = event.target;

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

        console.log(this.state.currentitem);

    }



    render() {

        return (

            <div>

                <form onSubmit={this.handleSubmit}>

                    <input type='text' placeholder='enter text' name='currentitem' onChange={this.handleChange}

                           value={this.state.currentitem}/>

                    <button type='submit'>Submit</button>

                </form>

            </div>

        )

    }

}


export default App;


查看完整回答
反對 回復 2022-07-08
  • 2 回答
  • 0 關注
  • 245 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號