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: Class component with defaultProps not merging props correctly in componentDidMount #28505

Open
amitdahan opened this issue Mar 6, 2024 · 3 comments

Comments

@amitdahan
Copy link

amitdahan commented Mar 6, 2024

React version: 18.2.0

Steps To Reproduce

  1. Have a lazy-wrapped, class-component with defaultProps, componentDidMount and componentWillUnmount.
  2. Using StrictMode, on initial call to componentDidMount, this.props contains defaultProps correctly.
  3. On 2nd mount - this.props isn't merged with defaultProps correctly.
const LazyComponent = lazy(
  async () =>
    class SomeComponent extends Component {
      static defaultProps = { someProp: true };
      componentDidMount() {
        // 1st mount - `true`
        // 2nd mount - `undefined`
        console.log(this.props.someProp);
      }
      componentWillUnmount() {}
      render() { /* ... */ }
    },
);

<StrictMode>
  <LazyComponent />
</StrictMode>

Link to code example:
https://stackblitz.com/edit/vitejs-vite-1ptuah?file=src%2Fmain.jsx

The current behavior

The StrictMode-triggered 2nd mount doesn't correctly merge defaultProps into this.props.

The expected behavior

this.props contains the specified defaultProps.

More context

I noticed each of the following works-around the issue:

  • Wrapping the class-component in a function-component (before wrapping with lazy)
  • Not using lazy
  • Not defining a componentWillUnmount

Since this is happening in a 3rd-party which we wrap with lazy, the 1st option is a solid workaround for now.

Possibly related issues

@amitdahan amitdahan added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Mar 6, 2024
@eps1lon eps1lon added Type: Bug and removed Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug labels Mar 6, 2024
@eps1lon
Copy link
Collaborator

eps1lon commented Mar 6, 2024

Thank you for the bug report and detailed repro! It also reproduces in Canary and Experimental release channels.

Keep in mind that this would only affect development since StrictMode has no effect in production.

@HermanBide
Copy link

has this been resolved?

@ayroblu
Copy link

ayroblu commented May 10, 2024

Strange, I notice this on non StrictMode too

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

No branches or pull requests

4 participants