Hi I am a beginner in React and trying to build my first app
login.js
import React from 'react' import LoginForm from './loginForm' import SignUpForm from './signUpForm' import Home from './home' class LoginPopUp extends React.Component { constructor(props) { super(props); this.state = { signup: false, login: true, loginSuccess: false, } this.renderLogin = this.renderLogin.bind(this) this.renderSignUp = this.renderSignUp.bind(this) this.uponSuccessfulLogin = this.uponSuccessfulLogin.bind(this) } renderSignUp() { this.setState({signup: true, login: false}) } renderLogin() { this.setState({signup: false, login: true}) } uponSuccessfulLogin() { this.setState({loginSuccess: true}) } render() { if (this.state.signup) { return <SignUpForm renderLogin={this.renderLogin}/> } else if (this.state.login) { return <LoginForm renderSignUp={this.renderSignUp} uponSuccessfulLogin={this.uponSuccessfulLogin}/> } else if (this.state.loginSuccess) { return <Home/> } } } export default LoginPopUp; loginForm.js
import React from 'react' import { post } from 'axios'; class LoginForm extends React.Component { constructor(props) { super(props); this.state ={ username: '', password: '', loginSuccess: '', submit: false, } this.onUsernameChange = this.onUsernameChange.bind(this) this.onPasswordChange = this.onPasswordChange.bind(this) this.onLoginSubmitButton = this.onLoginSubmitButton.bind(this) this.authenticate = this.authenticate.bind(this) this.handleButtonClick = this.handleButtonClick.bind(this) } onUsernameChange(e) { this.setState({username: e.target.value}) } onPasswordChange(e) { this.setState({password: e.target.value}) } componentDidUpdate() { if (this.state.loginSuccess) { if (this.state.loginSuccess === 200) { this.props.uponSuccessfulLogin() } else { alert("Authentication Failed! Provide valid credentials.") } } } authenticate() { const url = 'http://127.0.0.1:8000/api-token-auth/'; const formData = new FormData(); formData.append('username', this.state.username); formData.append('password', this.state.password); const config = { headers: { 'content-type': 'multipart/form-data' } } post(url, formData, config) .then(response => response.status) .then(status => this.setState({loginSuccess: status})) } onLoginSubmitButton() { this.authenticate() } handleButtonClick() { this.props.renderSignUp() } render() { if (!this.state.loginSuccess){ return ( <div> <h1>Login</h1> <form className='LoginForm' onSubmit={this.onLoginSubmitButton}> <label> Username: <input onChange={this.onUsernameChange}/> </label> <br/> <label> Password: <input type='password' onChange={this.onPasswordChange}/> </label> <br/> <input type='submit' value='Submit'></input> </form> <br/><br/><br/> <span>Not a member? </span> <button id="clickHere" onClick={this.handleButtonClick}> Click here to SignUp </button> </div> ); } } } export default LoginForm; When post (.then(status => this.setState({loginSuccess: status}))** ) is successful I expect componentDidUpdate is called and <Home/> is rendered but here it doesn't render <Home/> instead I get same login page. Please help me understand what did I do wrong.**
catchblock with thethenblockrenderfunction returns something. It can benullbut notundefinedlike now.