You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[react] Lit Element in React Component Doesn't Apply Default Prop Value on Re-render without Property Set #4227
I am currently experiencing a problem with a component's conditional rendering. During the first render, the component features a property with a particular value. However, during the subsequent render, the property is not set, and the lit element employs the previously set value instead of the default value. I would like to apologize if my explanation is vague or unclear.
Reproduction
You can see in the following sandbox that the Lit Element is expected to behave the same way as the React Component with default props. However, even though the component changes, the rendered values never change.
When a property is no long present in the pros object, there's really no way to "remove" it from the element. The best we could do here is set it to undefined, though we would have to remember previous values in order to do that.
I like the defaults idea though... we have been considering something similar for restoring a value when you remove an attribute. If we add that then maybe React wrappers could utilize it automatically.
This is a good observation/report, and due to a fundamental difference in how we treat "default" values in LitElement. The "default" are really just initial values set on element construction only, and not used in subsequent updates. We'd need to store this initial/default value separately, add logic in lit-html to detect the "removal" of an attribute/property on update and revert to default value.
The workaround at the wrapper creation layer is clever but not ideal as it requires duplicating the default props in the option.
We should at the very least document this behavior in the React integration docs, as it's different to what a React developer would expect.
The "problem" with this approach is that the createComponent handles the forwardRef, so to have access to the component ref, we would have to replicate the forwardRef handling being made on the createComponent it would require a lot of boilerplate code.
The workaround at the wrapper creation layer is clever but not ideal as it requires duplicating the default props in the option.
I've managed to reduce the duplication by using another strategy.
// content of the createComponent functiontypeConstructor<T>={new(): T;defaultProps?: Partial<T>};// omitting code for brevityconstReactComponent=React.forwardRef<I,Props>((props,ref)=>{constprevPropsRef=React.useRef<Props|null>((elementClass.defaultPropsasProps)??null,);// omitting code for brevity})
Kudos to @welingtonms who suggested this approach.
I was previously creating a const defaultComponentNameProps = { /* props and values here */ } as const and passing this constant as an argument to my custom createComponent function and manually setting each property value based on this object in the class constructor.
PS: The project I'm working on is not using decorators.
The use of static defaultProps is also how React class components do it.
We're probably not ready to commit to checking for it in the React wrapper generation as it implies a contract with the base custom element class (currently the wrapper isn't tied to LitElement/ReactiveElement though we could decide to change that), let alone it would have no meaning for standalone LitElements as is. If/when we add support for default property values in LitElement, it likely would involve the property option, rather than a separate defaultProp block like that.
I'm curious if you have direct, non-React wrapped, consumers of the custom elements too for your project and if they want this default property behavior as well?
changed the title [-][labs/react] Lit Element in React Component Doesn't Apply Default Prop Value on Re-render without Property Set[/-][+][react] Lit Element in React Component Doesn't Apply Default Prop Value on Re-render without Property Set[/+]on Jan 16, 2024
Hey @augustjk I am sorry about the delay. I forgot about this issue.
I'm curious if you have direct, non-React wrapped, consumers of the custom elements too for your project and if they want this default property behavior as well?
We don't have non-React wrapped consumers, but I believe we would like this behavior as well. At least for me, it makes sense.
Activity
justinfagnani commentedon Sep 26, 2023
When a property is no long present in the pros object, there's really no way to "remove" it from the element. The best we could do here is set it to
undefined, though we would have to remember previous values in order to do that.justinfagnani commentedon Sep 26, 2023
I like the defaults idea though... we have been considering something similar for restoring a value when you remove an attribute. If we add that then maybe React wrappers could utilize it automatically.
augustjk commentedon Sep 26, 2023
This is a good observation/report, and due to a fundamental difference in how we treat "default" values in
LitElement. The "default" are really just initial values set on element construction only, and not used in subsequent updates. We'd need to store this initial/default value separately, add logic inlit-htmlto detect the "removal" of an attribute/property on update and revert to default value.The workaround at the wrapper creation layer is clever but not ideal as it requires duplicating the default props in the option.
We should at the very least document this behavior in the React integration docs, as it's different to what a React developer would expect.
augustjk commentedon Sep 28, 2023
Another workaround on the consumer side without modifying the wrapper generator code is to simply wrap it with another React component that defaults the prop like this: https://codesandbox.io/p/sandbox/falling-rain-vw3cww?file=%2Fsrc%2FMyComponentWrapped.tsx
rivajunior commentedon Sep 28, 2023
The "problem" with this approach is that the
createComponenthandles the forwardRef, so to have access to the componentref, we would have to replicate theforwardRefhandling being made on thecreateComponentit would require a lot of boilerplate code.rivajunior commentedon Sep 28, 2023
I've managed to reduce the duplication by using another strategy.
Kudos to @welingtonms who suggested this approach.
I was previously creating a
const defaultComponentNameProps = { /* props and values here */ } as constand passing this constant as an argument to my customcreateComponentfunction and manually setting each property value based on this object in the class constructor.PS: The project I'm working on is not using decorators.
augustjk commentedon Sep 28, 2023
Indeed consumer wrapper would also need to use
forwardRef. I updated the sandbox https://codesandbox.io/p/sandbox/falling-rain-vw3cww?file=%2Fsrc%2FMyComponentWrapped.tsxThe use of
static defaultPropsis also how React class components do it.We're probably not ready to commit to checking for it in the React wrapper generation as it implies a contract with the base custom element class (currently the wrapper isn't tied to
LitElement/ReactiveElementthough we could decide to change that), let alone it would have no meaning for standaloneLitElements as is. If/when we add support for default property values inLitElement, it likely would involve the property option, rather than a separatedefaultPropblock like that.I'm curious if you have direct, non-React wrapped, consumers of the custom elements too for your project and if they want this default property behavior as well?
[-][labs/react] Lit Element in React Component Doesn't Apply Default Prop Value on Re-render without Property Set[/-][+][react] Lit Element in React Component Doesn't Apply Default Prop Value on Re-render without Property Set[/+]rivajunior commentedon May 17, 2024
Hey @augustjk I am sorry about the delay. I forgot about this issue.
We don't have non-React wrapped consumers, but I believe we would like this behavior as well. At least for me, it makes sense.
I created a new codesandbox https://codesandbox.io/p/devbox/jovial-silence-xph9nk with the updated packages and it looks like everything is good now. Thank you for fixing the issue.
I only had to add the following code to have the "default props" behavior
This is a workaround I would like to have an API to do this for me.