import { PureComponent } from 'react'; import { profileAsyncAutomat } from '../automats/profileAsyncAutomat.js'; let secondaryMountSequence = 0; const mountHistoryLog = []; /** * Secondary Consumer Component. * Unmounted and remounted by the parent collapsible toggle. * * Demonstrates: * When this component mounts while data is already in memory: * 1. `profileAsyncAutomat.getData()` returns { status: 'success', data } synchronously in `constructor()`. * 2. 1st render displays final data immediately with ZERO delay. * 3. `componentDidMount()` subscribes, but no state changes occur. * 4. RENDER COUNT STAYS AT 1! No 2nd render is necessary! */ class SecondaryProfileConsumer extends PureComponent { _renderCount = 0; _constructorStatus = 'uninitialized'; _mountId = 0; constructor(props) { super(props); this._mountId = ++secondaryMountSequence; // 1. 💡 Synchronous read in constructor: this.state = profileAsyncAutomat.getData(); this._constructorStatus = this.state.status; mountHistoryLog.unshift({ mountId: this._mountId, time: new Date().toLocaleTimeString(), statusInConstructor: this._constructorStatus, hadDataImmediately: this._constructorStatus === 'success', }); } componentDidMount() { // 2. Subscribe for updates (e.g. if Primary triggers a refetch or reset): this.unsubscribe = profileAsyncAutomat.subscribe(this); } componentWillUnmount() { if (this.unsubscribe) { this.unsubscribe(); } } render() { this._renderCount++; const { status, data } = this.state; const isSingleRenderSuccess = this._renderCount === 1 && status === 'success'; return (
status: 'success').
{data.endpoint} (Ping: {data.pingMs}ms)
Toggle the collapsible below to completely unmount and remount this
component. When data is already loaded in profileAsyncAutomat,
its constructor immediately receives the cached payload — rendering in 1 render with no 2nd render.