I read a lot about the differences between React component, stateless functional component, pure Component etc ...
I m curious about the use of method instead of stateless functional component, because i didnt found any information about it.
Here is my code, i m making a simple login class
// --------------------------- //
// -- LOGIN REACT COMPONENT -- //
// --------------------------- //
export default class Login extends React.Component {
constructor(props) {
super(props);
this.dataLoaded = false;
this.state = {
login : 'pending', // 3 values : success, fail, pending => 3 differents render
verify : false // could trigger visual cue on loading page when switched to 'true'
};
// Verify User if provided
this.verifyUserToken(userData.userId, userData.token);
}
//////////////////
// -- RENDER -- //
//////////////////
render() {
if (this.state.login === 'pending') return this.pendingPage();
if (this.state.login === 'success') return <App login={true} />; // ./App.js
if (this.state.login === 'fail') return this.loginForm();
return <div> This page should never load </div>;
}
// -- PENDING PAGE -- //
pendingPage() {
let loader = icons[10];
return (
<div id="login">
<img src={loader} alt="" className="loader" />
</div>
);
}
// -- LOGIN FORM-- //
loginForm() {
return (
<div id="login">
<form>
<div id="errorLogin" className="hidden">
{stringsLocalized.login.errorLogin}
</div>
<input type="text" id="pseudo" placeholder={stringsLocalized.login.pseudo} />
<input type="password" id="password" placeholder={stringsLocalized.login.password} />
<button type="submit" onClick={this.handleClickLogin}>
{stringsLocalized.login.button}
</button>
</form>
</div>
);
}
////////////////////
// -- HANDLERS -- //
////////////////////
handleClickLogin = e => {
e.preventDefault();
let pseudo = document.querySelector('#pseudo').value;
let password = document.querySelector('#password').value;
this.verifyUserPassword(pseudo, password);
};
If i follow react logic i should create stateless functional component for pendingPage & loginForm instead of method inside Login class.
Should I ? I couldn't find any information if this was a OK practice or not.
Any benefit of using stateless functional component in this case ?
stateless functional componentis not a recommended term. 1. function components are notstatelessany more starting v16.8.0. 2ndly,functional componentsdoesn't refer tofunction componentasfunctionalrefers toFunctional Programming. So just use the termfunction component🙂