简体   繁体   English

React Class 的方法代替 React 无状态功能组件

[英]React Class's method instead of React stateless functional component

I read a lot about the differences between React component, stateless functional component, pure Component etc ...我阅读了很多关于 React 组件、无状态功能组件、纯组件等之间差异的信息......

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, im 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.如果我遵循反应逻辑,我应该为 pendingPage 和 loginForm 创建无状态功能组件,而不是 Login 类中的方法。

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 component refers to component with no state.无状态组件是指没有状态的组件。 So where can we use them?那么我们可以在哪里使用它们呢? Answer is simple, if there is any UI component which needs no state.答案很简单,如果有任何不需要状态的 UI 组件。 For instance, you need to show list of image.例如,您需要显示图像列表。 In this component you don't have to use stateful component.在此组件中,您不必使用有状态组件。 You can simply make a stateless component something like this你可以简单地制作一个像这样的无状态组件

const ImageItem = () => {
   return (
      <img src='hi.png' width={100} height={100}/>
   )
} 

Now lets jump it to your case.现在让我们跳到你的情况。 Currently there is not state needed in pending page.目前在待处理页面中不需要状态。 But in case of login form you might need to make a stateful component.但是在登录表单的情况下,您可能需要制作一个有状态的组件。 State might be needed for validation and other purposes.验证和其他目的可能需要状态。

In case of stateless pending page, you can do create a new component and import in your current file.在无状态待处理页面的情况下,您可以创建一个新组件并导入当前文件。

    import React from 'react';

    const PendingPage = () => {
        const loader = icons[10];
        return (
            <div id="login">
                <img src={loader} alt="" className="loader" />
            </div>
        );
    }

    export default PendingPage;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM