2
const LoginPage = ({ auth, doLogin, doLogout }) => (


    <div>
      <NavBar auth={auth}/>
        <div className="row">
            <div style={{display: 'flex', justifyContent: 'center'}} className="col-md-4 col-md-offset-4">
                <LoginForm auth={auth} doLogin={doLogin} doLogout={doLogout}/>
            </div>
        </div>
      </div>

);

// Connects state to the props of this component
const mapStateToProps = state => ({
  auth: state.auth,
});

// Return a NEW component that is connected to the Store
export default connect(mapStateToProps, { doLogin, doLogout })(LoginPage);

Above is the code of the component called LoginPage. It passes in the global state auth and two actions doLogin and doLogout. The LoginForm handles the user authentication, but I want to put another component called Dashboad and render it conditionally based on the auth's status.

However, I am not sure how to conditionally render in a pure component.

I could change into a non-pure component, but then I am not sure how to pass in auth, doLogin, and doLogout into a non-pure component.

Please help.

1 Answer 1

1

You can do almost everything you did in a non pure component in a pure component, except that PureComponent automatically does the shouldComponentUpdate by react itself to avoid unnecessary rerenders based on the shallow diffing of state and props.

If you want to conditionally render dashboard you can check the auth status from redux and use this.props.history.push("/dashboard") or <Redirect to="/dashboard"> to navigate to Dashboard component

Sign up to request clarification or add additional context in comments.

2 Comments

Then how can I do conditional rendering in my example above?
You should change the above component from a functional component to a Component or a PureComponent and inside the comonentDidMount or componentWillReceiveProps, you can check thee authStatus, and use the history or redirect to navigate to dashboard

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.