mardi 30 mars 2021

Add a property in javascript class component in order to communicate between method

As title say and below indicate

class T extends React.Component {
    constructor(props) {
        super(props)
        this.a = false;
    }

    methodA {
    //will set 'a' in this method, maybe async.
    }

    methodB {
    // will get 'a' in this method.
    }

    render() {
        return (
                   <div>hello,world</div>
        )
    }
}

This looks like a very simple code. However, as the component become more complicated and the logic ascend, we need to add more property to the class to satisfy the communication of the method, like:

> class T extends React.Component {
>      constructor(props)
>        this.a = false;
>        this.b = 123;
>        this.c = 'string';
>        this.d = null;
>        this.e = [];
>        ...
>        this.z = ........
>      }

Apparently, it is not decent. So, could you please inform me do we have any other solution to replace that? Do we have any decent way to communicate between method in one class? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire