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

[Bug] Production build won't re-render if a component has no state or props #3

Open
loganvolkers opened this issue Mar 2, 2021 · 1 comment

Comments

@loganvolkers
Copy link
Member

Ran into a bug while using @saasquatch/stencilbook -- the component renders in dev mode, but when built for production the component will not re-render when state changes.

Upon deeper investigation the forceUpdate method from @stencil/core is being called, but render never gets called again.

By looking into the source of forceUpdate, it relies on an isUpdateable flag or becomes a no-op:

Deeper introspection shows this telling line:

  cmpMeta.isUpdateable = cmpMeta.hasProp || cmpMeta.hasState;

To reproduce, do a production build on this component:

import { withHooks, useState } from '@saasquatch/stencil-hooks';

@Component({
  tag: 'my-counter',
})
export class Counter {
  constructor() {
    withHooks(this);
  }

  render() {
    const [count, setCount] = useState(0);
    return (
      <div>
        {count} <button onClick={() => setCount(count + 1)}>+1</button>
      </div>
    );
  }

  disconnectedCallback() {
    // required for `useEffect` cleanups to run
  }
}

To fix add any unused prop or state.

import { withHooks, useState } from '@saasquatch/stencil-hooks';

@Component({
  tag: 'my-counter',
})
export class Counter {
   @Prop() unusedButAllowsForceUpdate = true;

   // ... rest of component ...
}
@loganvolkers
Copy link
Member Author

For others that have run into this, the current workaround is to create fake internal state.

   @State() ignore = true;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant