简体   繁体   English

React 中的表单提交消息

[英]Form Submit Message in React

I am trying to have a message appear with "You are now subscribed to {SUBSCRIPTION}."我正在尝试显示一条消息“您现在订阅了 {SUBSCRIPTION}。” when a user submits a form in React.当用户在 React 中提交表单时。 I am very new to React, so I feel like I am missing something small (hopefully).我对 React 很陌生,所以我觉得我错过了一些小东西(希望如此)。

So far, I have something that looks like this:到目前为止,我有一些看起来像这样的东西:

constructor(props) {
        super(props);

        this.state = {
            user: {
                newsletter: props.newsletter
            }
        }
    }

    handleNewsletterInput(event) {
        var user = this.state.user;

        user.newsletter = event.target.value;

        // Update the state object
        this.setState({ user: user })
    }

    handleSubmit(event) {
        var user = this.state.user;

        user.newsletter = event.target.value;

        alert("You are now subscribed to " + { user } + ".")
    }

    ...

    render() {
        return (
            <div>
                <h2>Subscribe</h2>
                <form onSubmit={this.handleSubmit.bind(this)}>
                    <label>
                        <p>Newsletter Type:</p>
                        <input
                            id='newsletter'
                            type='text'
                            name='newsletter'
                            value={this.state.user.newsletter}
                            onChange={this.handleNewsletterInput.bind(this)}
                        />
                    </label>
                    <button
                        type='submit'
                        id='subscribe'
                        name='subscribe'>
                        Subscribe
                    </button>
                </form>
            </div>
        );

     ...

When I run it, I get a message saying "You are now subscribed to [object Object]."当我运行它时,我收到一条消息说“您现在订阅了 [object Object]”。 What am I doing wrong, and how can I get [object Object] to say the name of the newsletter?我做错了什么,如何让 [object Object] 说出时事通讯的名称?

This is not a React issue.这不是 React 问题。

alert("You are now subscribed to " + { user } + ".")

Here you are concatenating an object with a string.在这里,您将一个对象与一个字符串连接起来。 The object is coerced into "[object Object]" string because any object v.toString() gives this by default(unless explicitly set), as its a memory reference.该对象被强制转换为 "[object Object]" 字符串,因为任何对象 v.toString() 在默认情况下(除非明确设置)都将此作为其内存引用。 Try this line instead:试试这一行:

alert("You are now subscribed to " + JSON.stringify(user) + ".")

and how can I get [object Object] to say the name of the newsletter?以及如何让 [object Object] 说出时事通讯的名称?

alert("You are now subscribed to " + user.newsletter + ".")

Unfortunately, I receive an error when I do that.不幸的是,我在执行此操作时收到错误消息。 alert("You are now subscribed to " + { user.newsletter } + ".")

This syntax you have mentioned in the comment below question is wrong.您在下面问题的评论中提到的这种语法是错误的。 {user.newsletter} is not correct syntax, it is neither an object nor a string. {user.newsletter}不是正确的语法,它既不是对象也不是字符串。

Edit after handleNewsletterInput added:添加handleNewsletterInput后编辑:

handleNewsletterInput(event) {
        var user = this.state.user;

        user.newsletter = event.target.value;

        // Update the state object
        this.setState({ user: user })
}

The above will not update state because you have not changed the memory reference of user variable.以上不会更新状态,因为您没有更改user变量的内存引用。 React will not know that the state has changed as it will do comparison prevState===currentState and it will give true , as its in the same memory location. React 不会知道状态已经改变,因为它会做比较prevState===currentState并且它会给出true ,因为它在相同的内存位置。 Try this instead:试试这个:

handleNewsletterInput(event) {
        const { user } = this.state;

        const updatedUser = {
            ...user,
            newsletter: event.target.value
        }

        // Update the state object
        this.setState({ user: updatedUser })
}

Edit 2: You've also made a mistake here -编辑 2:你在这里也犯了一个错误 -

handleSubmit(event) {
        var user = this.state.user;

        user.newsletter = event.target.value;

        alert("You are now subscribed to " + { user } + ".")
}

This event is the submit event, you do not need the first two lines here.这个事件就是提交事件,这里不需要前两行。

handleSubmit(event) {
        // Do something like send form data to backend here.

        alert("You are now subscribed to " + this.state.user.newsletter + ".")
}

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

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