I'm bit new to react i just wanted to know that how can add conditional rendering to a button in my login page, whenever username and password matches to the api username and password, after hitting submit button it should give me new component.
//here is api
const api ='http://localhost:3000/api/login';
//Login Component
export class Login extends Component {
state = {
username: '',
password: '',
confirmuser:'',
confirmpass:'',
};
componentDidMount() {
axios.get(api).then(response => {
let number = response.data.username;
console.log(response);
let number2 = response.data.password;
this.setState({ confirmuser:number});
this.setState({ confirmpass: number2 });
})
.catch((err)=>{
console.log('Looks like something went wroong ',err);
});
}
//HandleClick Method
handleClick=()=>{
const username = this.state.username;
const password= this.state.password;
const confirmuser=this.state.confirmuser;
const confirmpass=this.state.confirmpass;
console.log(confirmuser);
console.log(username);
console.log(password);
console.log(confirmpass);
if (username == confirmuser && password == confirmpass ){
console.log(`username is ${username}`);
}
else{
console.log('Please enter right username or password');
}
};
//Rendaring the elements from material-ui
render() {
return (
<div>
<div>
<AppBar
title="Login"
/>
<TextField
hintText="Enter your Username"
floatingLabelText="Username"
onChange={(event, newValue) => this.setState({ username: newValue })}
/>
<br />
<TextField
type="password"
hintText="Enter your Password"
floatingLabelText="Password"
onChange={(event, newValue) => this.setState({ password: newValue })}
/>
<br />
<RaisedButton label="Submit" primary={true} style={style} onClick={ (event) => this.handleClick(event)} />
</div>
</div>
);
}
};
How can i render after clicking button submit
handleClickis driving me crazy. You can shorten the assignments to a single line by using ES6 object descructuring :).const {username, password, confirmuser, confirmpass} = this.state