samedi 30 mars 2019

Constructor runs once in multi-Instantiates of a class in "builder pattern" in react app

I have a class in my react app which uses 'Builder Pattern' and I've imported it in some nested components in my page. for example in parent component and child component, too.

But it seems it calls the class's constructor once! and in second instantiate it has existing data which I've added in previews in previews instantiate. (Not a new and clean instantiate!)

// [Builder pattern] 
class Requester {
    constructor() {
        this.definedHeaders = {}
        console.log('construct', this)

    }

    contentType(contenttype) {
        this.definedHeaders['Content-Type'] = contenttype
        return this;
    }

    async get(url) {
        // ...
    }

    async post(url, data = {}, isFormData = false) {
         // ...
    }
}

export default new Requester();

Aucun commentaire:

Enregistrer un commentaire