The constructor of my react component is getting called twice but I can't figure out why. I am using react-redux to store the language of my app. I have a function that set the default language base on browser's language. LoginPage is the first component that gets render so I called my function in its constructor. Basically what it does is compare and dispatch an action. When I checked my state with redux developer tools I saw that it was getting dispatched twice. I printed dummy data in the constructor and it gets printed twice too.
LoginPage.js
import React from 'react';
import {connect} from 'react-redux';
import {startLogin} from '../actions/auth';
import {setLanguage} from '../actions/lang';
export class LoginPage extends React.Component{
constructor(props){
super(props);
this.setDefaultLanguage();
console.log('i am constructor');
}
changeLanguage = (e) => {
const lan = e.target.value;
this.props.setLanguage(lan);
};
setDefaultLanguage = () => {
const defaultLanguage = navigator.language || navigator.userLanguage || 'en-US';
if(defaultLanguage == 'es'){
this.props.setLanguage(defaultLanguage);
}else{
this.props.setLanguage('en');
}
}
render(){
return(
<div className="box-layout">
<div className="box-layout__box">
<h1 className="box-layout__title">Expensify</h1>
<p>It\'s time to get your expenses under control.</p>
<button className="button" onClick={this.props.startLogin}>Log in with google</button>
<select className="select" onChange={this.changeLanguage}>
<option value="en">English</option>
<option value="es">Español</option>
</select>
</div>
</div>
)
};
}
const mapDispatchToProps = (dispatch) => ({
startLogin: () => dispatch(startLogin()),
setLanguage: (language) => dispatch(setLanguage(language))
});
export default connect(undefined, mapDispatchToProps)( LoginPage);
App.js
import React from 'react';
import ReactDom from 'react-dom';
import {Router, Route, Switch} from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';
import ExpenseDashBoardPage from '../components/ExpenseDashBoardPage';
import AddExpensePage from '../components/AddExpensePage';
import EditExpensePage from '../components/EditExpensePage';
import NotFoundPage from '../components/NotFoundPage';
import LoginPage from '../components/LoginPage';
import PrivateRoute from './PrivateRoute';
import PublicRoute from './PublicRoute';
export const history = createHistory();
const AppRouter = () => (
<Router history={history}>
<div>
<Switch>
<PublicRoute path="/" component={LoginPage} exact={true} />
<PrivateRoute path="/dashboard" component={ExpenseDashBoardPage} />
<PrivateRoute path="/create" component={AddExpensePage} />
<PrivateRoute path="/edit/:id" component={EditExpensePage} />
<Route component={NotFoundPage} />
</Switch>
</div>
</Router>
)
export default AppRouter;
Yep. It’s intentional and documented here. It only happens in development mode.
https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects
I was having the same issue because I was redirecting the user inside my public routes. So it was rendering the component twice. In case you are using
history.push('/');
This is going to render the component twice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With