出现在网址中的查询起反应

时间:2018-02-27 19:25:51

标签: javascript reactjs react-router

我正在使用HashRouter在React上制作应用。在应用程序中,我有一个带输入的表单,在用户提交数据后,一些数据显示在URL中:

http://localhost:3000/?State=Alabama#/profile

当它应该只是

http://localhost:3000/#/profile

提交按钮上的功能只是获取所有值并发送到数据库:

    handleSubmit() {
        const { userUrl } = this.props;
        let { street_address, state, city, zip, email, phone, avatar, about_message, proximity } = this.state;

        if (this.props.user.title === 'caregiver') {
            axios.put('/update/profile', {
                street_address,
                state,
                city,
                zip,
                email,
                phone,
                avatar: userUrl.length ? userUrl : avatar,
                about_message,
                proximity
            })
                .then(response => {
                    console.log(response);
                    this.setState({
                        street_address,
                        state,
                        city,
                        zip,
                        email,
                        phone,
                        avatar,
                        about_message,
                        proximity
                    })
                })
                .catch(error => console.log(error))
        }
    }

我发现问题部分存在于表单中,因为某些输入的名称=""属性,如果我把它拿出来,它就不会显示在URL中。但问号仍在出现。

1 个答案:

答案 0 :(得分:2)

您需要阻止表单进行默认提交:

handleSubmit(e) {
        e.preventDefault()
        const { userUrl } = this.props;
        let { street_address, state, city, zip, email, phone, avatar, about_message, proximity } = this.state;

        if (this.props.user.title === 'caregiver') {
            axios.put('/update/profile', {
                street_address,
                state,
                city,
                zip,
                email,
                phone,
                avatar: userUrl.length ? userUrl : avatar,
                about_message,
                proximity
            })
                .then(response => {
                    console.log(response);
                    this.setState({
                        street_address,
                        state,
                        city,
                        zip,
                        email,
                        phone,
                        avatar,
                        about_message,
                        proximity
                    })
                })
                .catch(error => console.log(error))
        }
    }