Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render() is not getting called after UI changes programmatically #55

Open
ankushbkadam opened this issue Dec 4, 2020 · 0 comments
Open

Comments

@ankushbkadam
Copy link

ankushbkadam commented Dec 4, 2020

I am creating a toggled UI
It will toggle between headers original UI and my custom UI depend upon a global variable's value.

So I created a block setting to toggle in between

So initially block will have only header element,
UI1:
<h2></h2>

after toggled it should be like
UI2:

<div class="newWrapper">
    <h2></h2>
    <div>my contents here</div>
</div>

after implementing this, it is working fine at first, but not toggling back later.

initially, UI1 is getting rendered then after toggling, UI2 is getting rendered
but after toggling again it should go back to UI1 but in this case, render is not getting called again and UI2 is always getting rendered.

In this scenario render is not getting called again in order to render new UI with conditions, is there any solution?

Following is the way I implemented it:

I have taken a global variables:
One to hold the anchorEditorWrapper toggle state which will be false initally
this._showAnchorEditor = false;``
and a wrapper div
this.wDiv = this.getUI();

So my render() method will be,

render() {
    return this.wDiv;
}

I have written a getUI() method, which will return appropriate UI depending on global flag variable as,

getUI(){
 return this._showAnchorEditor ? this.getWdiv() : this._element;
}

this._showAnchorEditor will be toggled using the block actions button.
this button will then call show() or hide() methods depend on this._showAnchorEditor variable state

In show() or hide() methods, I get new UI by using getUI() method and re-assign it to the global variable this.wDiv

showAnchorEditor() {
    this._showAnchorEditor = true;
 
   //create new UI
    const newWDiv = this.getUI();
    this.wDiv.parentNode.replaceChild(newWDiv, this.wDiv);
}
hideAnchorEditor() {
    this._showAnchorEditor = false;
    const newWDiv = this.getUI();
    this.wDiv.parentNode.replaceChild(newWDiv, this.wDiv);
    this.wDiv = newWDiv;
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant