question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

lit-element props configured by TS decorators do not cause a re-render of the component

See original GitHub issue

Describe the bug With Lit-element, using controls to update the element’s properties, storybook does not cause a render of the component when the component’s properties are defined by TypeScript decorators.

To Reproduce https://github.com/steveworkman/sb6-ts-decorators-bug

  1. In that repo - run the storybook
  2. In the non-decorators story, change the “kind” to be “secondary” - see that the component re-renders and the colour changtes
  3. Do the same in the decorators story

Expected behavior The component should re-render the same as the non-decorator one

Code snippets Repo: https://github.com/steveworkman/sb6-ts-decorators-bug

System:

Environment Info:

  System:
    OS: macOS 10.15.6
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
  Binaries:
    Node: 12.16.1 - ~/.nvm/versions/node/v12.16.1/bin/node
    npm: 6.14.7 - ~/.nvm/versions/node/v12.16.1/bin/npm
  Browsers:
    Chrome: 85.0.4183.83
    Safari: 13.1.2
  npmPackages:
    @storybook/addon-actions: ^6.0.21 => 6.0.21 
    @storybook/addon-essentials: ^6.0.21 => 6.0.21 
    @storybook/addon-links: ^6.0.21 => 6.0.21 
    @storybook/web-components: ^6.0.21 => 6.0.21 

Additional context I think it’s to do with how the TS decorators are transpiled, but I can’t be sure. This was working in SB 5 with a webcomponents setup.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
MaxAltenacommented, Sep 24, 2020

The proposed fix by @emilio-martinez works for me. I added a babel.config.js into the .storybook/ directory with the plugins. Thanks!

4reactions
emilio-martinezcommented, Sep 24, 2020

I’m not able to fully dig in now, but this does sounds familiar. I believe this is a Babel-related issue when applying the following combination of options within the Babel config:

// Storybook is likely using these options within its Babel
"plugins": [
  ["@babel/plugin-proposal-decorators", { "legacy": true }],
  ["@babel/plugin-proposal-class-properties", { "loose": true }]
]

TL;DR, use these options instead

"plugins": [
  [ "@babel/plugin-proposal-decorators", { "decoratorsBeforeExport": true } ],
  "@babel/plugin-proposal-class-properties"
]

LitElement has two decorator implementations, attempting to support two different stages of decorator proposals. The Typescript compiler (tsc) uses the older proposal, while Babel’s @babel/plugin-proposal-decorators by default will use the newer one. However, @babel/plugin-proposal-decorators optionally allows for a legacy flag, which instead switches to support the same proposal used with tsc.

However, that legacy option from Babel doesn’t work well with LitElement decorators + Typescript. Instead, when using Babel you should target the non-legacy variant and enable the decoratorsBeforeExport option as shown above. I do remember useDefineForClassFields being an issue in some cases, as Justin mentioned, but I don’t remember that being related to this particular issue. Incidentally, Babel applies that behavior via @babel/plugin-proposal-class-properties.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Properties
A property change can trigger an asynchronous update cycle, which causes the component to re-render its template. When a property changes, the following ......
Read more >
lit-element not changing view after property value change
This may be why a change to selName is not getting you a re-render of simple-el with an updated user property, because you...
Read more >
How to Observe Property Changes with LitElement and ...
Also, I explained how LitElement manages the declared properties and when it triggers an update cycle, causing the component to re-render ...
Read more >
Args - Storybook - JS.ORG
Args can be used to dynamically change props, slots, styles, inputs, etc. It allows Storybook and its addons to live edit components. You...
Read more >
Lit for React Developers
Components & Props; State & Lifecycle; Hooks; Children; Refs; Mediating State. What you'll build. At the end of this codelab will be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found