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

False positive getDefaultProps warning when mixing development and production versions #9999

Open
mondwan opened this issue Jun 19, 2017 · 39 comments

Comments

@mondwan
Copy link

mondwan commented Jun 19, 2017

As migrating from 15.4 to 15.5, I am rewrite original react components with create-react-class.

However, there is a warning.

react-15.5.4.js?bust=1497843639843:3287 Warning: getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.

As I am really transferring from a classic React.createClass definition, I would like to ask is this warning appropriate? Or, this is a depreciation sames as the level of migrating to create-react-class?

Ooops, I get ticket #9999 :D

@aweary
Copy link
Contributor

aweary commented Jun 19, 2017

@mondwan can you share an example reproducing the issue? I'm not seeing the warning when using the latest create-react-class version, or the last 15.5 release. Example. You can use this JSFiddle as a starting point for reproducing.

@mondwan
Copy link
Author

mondwan commented Jun 20, 2017

https://jsfiddle.net/84v837e9/105/

For non minified version, it works as expected. However, it does not if using minified version

Maybe related to issue #9689?

@aweary
Copy link
Contributor

aweary commented Jun 20, 2017

@mondwan the issue is that you're using the minified create-react-class build, but the unminified react build. This means that the internal flag that create-react-class uses to suppress this warning is not applied, so React warns.

Make sure you use the correct build for your environment (minified for production, unminified for development).

@aweary aweary closed this as completed Jun 20, 2017
@mondwan
Copy link
Author

mondwan commented Jun 20, 2017 via email

@aweary
Copy link
Contributor

aweary commented Jun 20, 2017

Because getDefaultProps is only used for components created with createReactClass. For example, the warning will catch cases where someone is moving from createClass to ES6 classes and they forget to remove getDefaultProps in favor of defaultProps.

You'll never get the warning when using the correct build of create-react-class. It's also important to use minified bundles for react and react-dom in production, as they're much more performant. On the same token, it's useful to use the unminified build in development as it contains a sizeable set of useful checks and warnings.

@mondwan
Copy link
Author

mondwan commented Jun 21, 2017 via email

@aweary
Copy link
Contributor

aweary commented Jun 21, 2017

You're getting the warning because you're using the production create-react-class build with the development react build. The production build strips out the code that prevents this warning in development

@gaearon
Copy link
Collaborator

gaearon commented Jun 21, 2017

In general we don't support mixing production and development versions of libraries.

@gaearon
Copy link
Collaborator

gaearon commented Jun 21, 2017

It would still be nice if there was a way to avoid the false positive as this is pretty confusing. Maybe we can turn a flag into a number, and always set it in DEV. If it's not set then we're in PROD mode and shouldn't check.

@gaearon gaearon reopened this Jun 21, 2017
@mondwan
Copy link
Author

mondwan commented Jun 21, 2017 via email

@gaearon
Copy link
Collaborator

gaearon commented Jun 21, 2017

No, to fix the false positive warnings, you need to either use:

  • react.js and create-react-class.js (development)
  • react.min.js and create-react-class.min.js (production)

The mistaken warning occur when you mix development and production versions of these two libraries.

@mondwan
Copy link
Author

mondwan commented Jun 21, 2017 via email

@gaearon
Copy link
Collaborator

gaearon commented Jun 21, 2017

Both of them work.

But even without changing it, it will still work correctly with createClass. The only issue is mistaken warning. And that mistaken warning happens because you mix development and production versions.

@gaearon gaearon changed the title Question on react's warning getDefaultProps False positive getDefaultProps warning when mixing development and production versions Jul 11, 2017
@elCarda
Copy link

elCarda commented Jul 25, 2017

@gaearon If I do understand it correctly that this can also affect the case when create-react-class is loaded via webpack and minified using uglifyjs during build with whole bundle from sources. The internal token can be named differently -> dozens of false positive warnings in console.

@gaearon
Copy link
Collaborator

gaearon commented Jul 25, 2017

It is not super clear to me what you mean. Uglify doesn't mangle property names because it is generally unsafe.

@gaearon
Copy link
Collaborator

gaearon commented Jan 5, 2018

The action item here is to:

  1. Reproduce the false positive when mixing development and production versions as described below.
  2. Figure out why it happens. You can check create-react-class source code on 15-stable branch. As described above it probably happens because we set some flag only in development.
  3. Figure out a fix.

@sportnak
Copy link

I'll try looking into this one. I'll update if I'm unable to make any progress. Thanks!

@sportnak
Copy link

I've opened a PR for this. react-with-addons.[min].js will warn if isReactClassApproved is null. create-react-class only sets this in development mode, but I think it should be set in both. So I removed the check. Running the test suite shows everything passing as before, so it still warns when it should. But I was uncertain on how to add a production script to a dev build.

Here's the test I used that reproduces the error.

    spyOn(console, 'error');
    var altCreateReactClass = require('./create-react-class.js');
    if (process.env.NODE_ENV !== 'production') {
      altCreateReactClass = require('./create-react-class.min.js');
    }

    var Component = altCreateReactClass({
      getDefaultProps: function() {
        return {
          foo: 0
        };
      },

      render: function() {
        return <span />;
      }
    });

    ReactDOM.render(React.createFactory(Component)(), document.createElement('div'));
    expect(console.error.calls.count()).toBe(0);

@benschac
Copy link

@mondwan @gaearon I'd like to grab a good-first-issue. This one looks a bit stale. If it's still needed I'd love to take a crack at it.

@gaearon
Copy link
Collaborator

gaearon commented Aug 14, 2018

Sure, feel free to look at it. Note #12064 is the most obvious fix but it doesn't actually solve the problem: we want to not do any extra steps in production.

@CharlieTruong
Copy link

@benschac Are you still poking at this? Would like to take a crack at it if it's not being worked on.

@benschac
Copy link

benschac commented Sep 16, 2018 via email

@ryansaam
Copy link

ryansaam commented Oct 1, 2018

I would like to take this as my good first issue

@CharlieTruong
Copy link

CharlieTruong commented Oct 1, 2018

I have a PR open (referenced above your comment) that attempts to address this issue. Awaiting feedback from @gaearon or someone else. Everyone on the core team likely very busy.

@ryansaam
Copy link

ryansaam commented Oct 4, 2018

Yeah I'm sorry I wasn't sure. I should've asked you first. I'm new to contributing so I am still trying to figure out how the flow of things work here. @CharlieTruong

@CharlieTruong
Copy link

@ryansaam no worries. I'm new to contributing as well so still trying to figure it out. Feel free to look at my PR and provide feedback. May I approached it wrong.

@garyxuehong
Copy link

Hi I made a PR for this issue. The way I did it is adding a flag for dev mode and skip the check if it is not in dev mode.

I need help for writing the test, especially how to test the mix development/release bundle in the test. Much appreciate if anyone can help.

I also has some questions:

  1. Should I make PR against branch 15-stable or master?
  2. Should I even discuss my changes before I make the PR so it won't wastes someone else's time?

Sorry I am a new comer for react codebase and OSS contribution.

ps: @CharlieTruong , hi my PR is highly inspired from yours. Only thing I changed is to use an explicit flag instead of implicitly rely on the existent of the getDefaultProps function.

@jamesyesware
Copy link

@garyxuehong The React contributions docs mention that PRs should always be made against master. Commits are cherrypicked for release branches later on. Looks like your PR is pointing to a stable version branch, though!

@garyxuehong
Copy link

@jamesyesware The file I fixed against is in 15-stable branch only (addons/create-react-class/factory.js). The file is not present in master branch. Does that mean this issue doesn't need to be fixed?

@JongminKimSyd
Copy link

@jamesyesware and @s-kennedy, it was already explained #12064 (comment) by @gaearon .

@george-veras
Copy link

Hi! Is this work still needed?
I'm looking to grab a first issue to contribute to, but after spending some time on this thread it seems to have been just left out.

@Stexxe
Copy link

Stexxe commented Feb 3, 2020

When function createClass is executed and there is getDefaultProps in the spec and we are not in production mode, then we approve that getDefaultProps is intentional. As was mentioned before we approve react class only in development mode, because of performance reasons, so we cannot approve it in any environment.

I think the problem is not in mixing development and production versions, but in relying on isReactClassApproved property. The error message says that: getDefaultProps is only used on classic React.createClass definitions, so from logic of the message we should warn only if we have getDefaultProps and element was created not from React.createClass, despite the fact of mixing environments.

The table below describes different states and where bug occurs.

react create-react-class bug
dev prod (isReactClassApproved is not set)
dev dev - (isReactClassApproved is set)
prod dev - (no check)
prod prod - (no check)

I have three possible ways in mind of how to fix that bug:

  1. We can add extra description of mixing environments issue to warning message to turn the bug into expected behaviour and make it obvious.
  2. We can indicate that type was created from React.createClass by adding some property (like __fromCreateClass) to type on any environment and then check that property exists instead of isReactClassApproved.
  3. From definition of React.createClass, if the spec has getDefaultProps then we execute that function and set its return value to defaultProps property. We could remove that function from Constructor after filling defaultProps if that won't hurt compatibility with older versions.

The question is how we suppose to fix this if there is no React.createClass in master branch, but only in branch 15.6-dev?

@rahul3002
Copy link

is anyone Working on this issue Can i?

@gsprasanna
Copy link

is this issue resolved?

@Shurtu-gal
Copy link

Hey @mondwan is this still relevant?

@mondwan
Copy link
Author

mondwan commented Jun 15, 2023 via email

@techwizard31
Copy link

I want to work on this issue, please give me a chance

@QQ07
Copy link

QQ07 commented Feb 12, 2024

@techwizard31 grow up bud!

@strategy155
Copy link

If the issue is not relevant, let's close it then :)

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