GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Nullstack
  • What is Nullstack?
  • Documentation
  • Examples
  • Contributors
  • F.A.Q
  • Waifu
  • Português
  • Star
  • Stateless Components
  • Stateful components
  • Full stack lifecycle
  • Server functions
  • Context

This view

  • Original page
  • New address

GeezerVison is showing nullstack.app — Persistent Components - Nullstack

Persistent Components

Persistent Components are components that persists on memory.

It preserves the state even when moved out from DOM and is good for saving whole app parts in cache.

It runs prepare and initiate lifecycle hooks only once.

It always runs hydrate when entering DOM and terminate when exiting.

import Nullstack from 'nullstack';
import Counter from './Counter';

class Application extends Nullstack {

  showCounter = true;
  toggleCounter() {
    this.showCounter = !this.showCounter;
  }

  render() {
    return (
      <div>
        {this.showCounter && <Counter persistent key="Counter"/>}
        <button onclick={this.toggleCounter}>Show/Hide Counter</button>
      </div>
    )
  }

}

export default Application;

Whenever showCounter becomes false, Counter exits the tree but it's state keeps the same when re-entering.

It also mantains the component in instances listing, making possible to manage it's state even when outside DOM:

import Nullstack from 'nullstack';
import Counter from './Counter';

class Application extends Nullstack {
  // ...
  updateCounter({ instances }) {
    instances.Counter.count += 1;
  }

  render() {
    return (
      <div>
        ...
        <button onclick={this.updateCounter}>
          Update Counter even outside the DOM
        </button>
      </div>
    )
  }
}

export default Application;

When re-entering the tree, the state is shown according to the changes made.

You can even dynamically enable/disable persistency, bringing back the default behavior:

import Nullstack from 'nullstack';
import Counter from './Counter';

class Application extends Nullstack {
  // ...
  persistentCounter = true
  tooglePersistency() {
    this.persistentCounter = !this.persistentCounter;
  }

  render() {
    return (
      <div>
        <Counter persistent={this.persistentCounter}/>
        <button onclick={this.tooglePersistency}>
          Toogle persistency in Counter
        </button>
      </div>
    )
  }
}

export default Application;

Next Step

➡️ Learn more about Context Keys: Routes and params❓ Have any questions or suggestions? Join our Discord
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.