Basic React form submit refreshes entire page

前端 未结 2 1843
忘掉有多难
忘掉有多难 2021-01-04 11:58

I\'m trying to create an input form that stores information in a component\'s state variable, then outputs that variable on the screen. I read the docs on controlled compone

2条回答
  •  醉梦人生
    2021-01-04 12:40

    Prevent the default behaviour:

    afterSubmission(event) {
        event.preventDefault();
        let name = this.state.itemName;
        this.setState ({
            storedItemName:this.state.itemName
        }, function() {
            alert(this.state.storedItemName); // Shows the right value!
        });
    }
    

提交回复
热议问题