3

I'm working on a react application wherein a user can register, and upon registering, the user would then be redirected to the home page.

Below is how I currently have my code.

The problem is after the user submits the registration form and the registration is successful, once the user is redirected I get the following warning message in the browser console:

[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

I tried two ways of redirecting, using history.push (passed as a prop from the Route component), and using the Redirect component once I update the state and RegisterForm is rerendered. The warning message is displayed regardless of which method I use.

My question is what am I doing wrong, and how do I fix this problem?

I've seen a lot of other questions regarding this issue, but they mostly just describe why this warning is getting displayed, but not necessarily how to fix the issue causing the warning.

class RegisterForm extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            loggedIn: false,
            username: '',
            email: '',
            password: '',
            passwordConfirmation: ''
        };

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

    handleChange(event) {
        const target = event.target;
        const name = target.name;
        const value = target.value;

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

    handleSubmit(event) {
        axios.post('/users/register', {
            email: this.state.email,
            username: this.state.username,
            password: this.state.password,
            password_confirmation: this.state.passwordConfirmation,
        })
        .then(response => {
            // const history = this.props.history;
            // history.push('/');

            this.setState({
                loggedIn: response.data.authenticated
            });
        })
        .catch(error => {
            console.log('Error registering user.');
        });

        event.preventDefault();
    }

    render() {
        const loggedIn = this.state.loggedIn;

        if (loggedIn) {
            return <Redirect push to="/" />
        }

        return (
            <form onSubmit={this.handleSubmit}>
                <div>
                    <label htmlFor="email">Email:</label>
                    <input type="email" id="email" name="email" value={this.state.email} onChange={this.handleChange} />
                </div>

                <div>
                    <label htmlFor="username">Username:</label>
                    <input type="text" id="username" name="username" minLength="3" maxLength="31" value={this.state.username} onChange={this.handleChange} />
                </div>

                <div>
                    <label htmlFor="password">Password:</label>
                    <input type="password" id="password" name="password" minLength="6" value={this.state.password} onChange={this.handleChange} />
                </div>

                <div>
                    <label htmlFor="passwordConfirmation">Confirm Password:</label>
                    <input type="password" id="passwordConfirmation" name="passwordConfirmation" minLength="6" value={this.state.passwordConfirmation} onChange={this.handleChange} />
                </div>

                <button type="submit">Register</button>
            </form>
        );
    }
}

function Register(props) {
    return (
        <div>
            <h1>Register</h1>
            <RegisterForm history={props.history} />
        </div>
    );
}

function App() {
    return (
        <div>
            <Router>
                <Header />

                <Switch>
                    <Route path="/register" component={Register} />
                    <Route path="/" component={Home} />
                </Switch>
            </Router>
        </div>
    );
}

ReactDOM.render(<App />, document.getElementById('app'));
user4181107
  • 321
  • 2
  • 17
  • Axios cannot make synchronous requests afaik, but the warning is about such a request. Do you have any code still using xmlhttprequest directly? Or is your version of react or other components like years old? My react knowledge is very limited, but I cannot identify the lines in the code shown that would make a synchronous request. – Shilly Oct 04 '19 at 13:33
  • @Shilly I'm using the latest version of everything. My `Home` component was loading some data from the backend, but it's also using axios, and I commented out that code to make sure it wasn't the cause for the warning message, which it wasn't. – user4181107 Oct 04 '19 at 13:40
  • Can you indentify which request for which file it is in the dev tools? https://stackoverflow.com/a/42058621/3049127 Apparently a lot of libraries that want to be compatible still use synchronous requests to be able to support old browsers. – Shilly Oct 04 '19 at 13:45
  • 5
    @Shilly I feel dumb for not checking that sooner. It was a browser extension causing the error... – user4181107 Oct 04 '19 at 13:51

0 Answers0