6

I have the following:

const RoutedApp = ({ signedIn }) => {
  return (
    <BrowserRouter>
      <Switch>
        <Route path={routes.HANDLE_SIGN_IN} render={props => <HandleSignIn {...props} />} />
        <PublicRoute exact path={routes.LANDING} signedIn={signedIn} component={LandingContainer} />
        <PrivateRoute exact path={routes.HOME} signedIn={signedIn} component={HomeContainer} />
        <Route component={PageNotFoundContainer} />
      </Switch>
    </BrowserRouter>
  );
};

The issue is with the HandleSignInContainer.

That component basically just signs the user in and sends them to the home page with location.push('/home');

However, I'm having the issue where HandleSignInContainer is mounting twice.

The docs mention that using the Route render prop should fix this problem, but then this SO answer mentions that this won't work for components created by an HOC.

Since HandleSignIn is wrapped with React Redux's connect(), how can I stop this component from mounting twice?

update 1

Trying to wrap in non-connected component:

const WrappedHandleSignInContainer = props => (
  <div>
    <HandleSignInContainer {...props} />
  </div>
);

update 2

Code for the HandleSignInContainer:

import React from 'react';
import { connect } from 'react-redux';
import { signInAction } from '../actions/userActions';
import { checkSignInLink } from '../resources/utils';

class HandleSignInContainer extends React.Component {
  state = {
    valid: true,
  };

  componentDidMount() {
    alert('mounted');

    const url = window.location.href;

    if (!checkSignInLink(url)) {
      this.setState({ valid: false });
      return;
    }

    let email = localStorage.getItem('email');
    this.props.signInAction(email, url, this.props.history);
  }

  render() {
    const { valid } = this.state;
    let text;

    if (valid) {
      text = "Please wait, we're signing you in!";
    } else {
      text = "Oops! That doesn't seem to be a valid sign in link.";
    }

    return text;
  }
}

export default connect(null, { signInAction })(HandleSignInContainer);
Colin Ricardo
  • 16,488
  • 11
  • 47
  • 80

0 Answers0