curriculum icon indicating copy to clipboard operation
curriculum copied to clipboard

Type Checking With PropTypes: PropTypes and defaultProps cannot be used in React version 19 and above

Open Punith1117 opened this issue 9 months ago • 19 comments

Checks

Describe your suggestion

Issue details:

  • Both PropTypes and defaultProps does not work in functional component
  • defaultProps works but PropTypes doesn't in class component

Possible ways to fix this:

  • [quickest] Mention this same exact fact in the beginning of the article so that people can be aware of this and most importantly preventing wastage of their time in figuring out why it doesn't work (they neither get an error nor a warning when using it) It could be fixed by installing react versions lesser than 19.
  • Remove the whole propTypes and defaultProps concept (this forces the curriculum to teach typescript in detail)

Path

Node / JS

Lesson Url

https://www.theodinproject.com/lessons/node-path-react-new-type-checking-with-proptypes

(Optional) Discord Name

No response

(Optional) Additional Comments

No response

Punith1117 avatar Mar 12 '25 15:03 Punith1117

Just adding some context here, vite's latest react template now uses react 19. So any project created with npm create vite@latest . -- --template react will not support type checking with proptypes

GaneshS288 avatar Mar 12 '25 15:03 GaneshS288

Regarding defaultProps, the Passing Data Between Components lesson was already amended in #28406 to mention defaultProps as an extra with class components and older codebases. Now that it's actually been removed from function components in React 19 (and not just giving a warning in React 18), perhaps the last sentence of that section could be tweaked to:

React now prefers the default parameter approach for function components and as of React 19, defaultProps will no longer work in function components. You may still see them used in class components or older codebases so it's still good to be aware of them.

As for propTypes, that'll definitely be a bigger team decision as to how to proceed, but I don't think any of this suggests that TypeScript is needed as a solution.

mao-sz avatar Mar 12 '25 15:03 mao-sz

React now prefers the default parameter approach for function components and as of React 19, defaultProps will no longer work in function components. You may still see them used in class components or older codebases so it's still good to be aware of them.

@MaoShizhong Isn't it better to mention what you said in the beginning instead of the end. Or at least somewhere in the middle? This will prevent readers from trying to run the code unnecessarily.

Punith1117 avatar Mar 12 '25 15:03 Punith1117

Depending on what a maint decides, the exact wording and where can of course be up for discussion and review. I wouldn't think much needs to be done regarding defaultProps, just perhaps something that makes it clearer it no longer works in function components in React 19 (because it'll still work in class components in React 19 as well as anywhere in older versions).

mao-sz avatar Mar 12 '25 16:03 mao-sz

Actually, this would require a lot of refactoring rather than just tweaking a sentence since the article directs the readers to "do things" by mentioning terms like setting up, typing the command to install the packages instead of just giving the information. It's a bigger issue than I imagined it to be.

Punith1117 avatar Mar 12 '25 16:03 Punith1117

I was referring specifically to the defsultProps issue which is a separate and much smaller thing to address.

The propTypes lesson is its own thing and yes, I agree is a much bigger issue with multiple long-term options, mostly rather big options. For the team, I'd suggest considering the purpose of the propTypes lesson within this React course and TOP. Given it physically cannot be used in React 19 anymore (and has been recommended against/deprecated for some time beforehand), is the lesson contents a necessity in this course?

mao-sz avatar Mar 12 '25 16:03 mao-sz

Even if it's only about defaultProps, the code snippet given there doesn't work since it's a functional component and not a class component.

Punith1117 avatar Mar 12 '25 16:03 Punith1117

Hence only a small tweak is actually necessary ti be more explicit about it not working in function components. That part of the section is already clear it's just showing something you might see elsewhere and will be even clearer it's not meant for the learner to implement themselves when it says it won't work in React 19 (and class components have not been introduced yet, so a class component example doesn't seem sensible).

Though the team may decide differently of course.

mao-sz avatar Mar 12 '25 16:03 mao-sz

Okay, looking forward to the team's final decision!

Punith1117 avatar Mar 12 '25 16:03 Punith1117

At some point in the future I think we'll want to either remove or at least significantly diminish the content, but at the moment I think just adding a note at the start of the lesson is sufficient since I would wager it will still be relevant knowledge for at least the next couple years.

Thanks for making this issue and volunteering to work on it @Punith1117, and thanks to all involved for the great discussion so far!

wise-king-sullyman avatar Mar 19 '25 01:03 wise-king-sullyman

Thanks @wise-king-sullyman for assigning me the task.

Image

I would also like to add that it's been asked to use this type checking in the shopping-cart project.

Punith1117 avatar Mar 19 '25 14:03 Punith1117

"

Note: propTypes and defaultProps are discontinued from React version 19. Please use React versions lesser than 19 to follow this lesson.

This can be done by:

  • Setting up Vite as usual
  • Changing the react version in package.json file before running 'npm install'

Change React version:

  • "dependencies": { "react": "^18.0.0", // Changed to React 18 "react-dom": "^18.0.0" // Changed to ReactDOM 18 }
  • "devDependencies": { "@types/react": "^18.0.10", // Updated to React 18 types "@types/react-dom": "^18.0.5", // Updated to ReactDOM 18 types }
  • Leave the rest in package.json file untouched

" @wise-king-sullyman Is this note good enough to add it here

Image

?

Punith1117 avatar Mar 22 '25 12:03 Punith1117

The link to the PropsTypes documentation resolves to nothing

PropsType Documentation

itskokeh avatar Apr 01 '25 12:04 itskokeh

bump.

I ran into this issue working through the "Type Checking With PropTypes" section today. This page should be updated with at least a note asap to stop confusion for readers. I spent a while this morning trying to figure out what I was doing wrong before finding the deprecation.

typies avatar Apr 08 '25 16:04 typies

@typies I faced the same exact problem. I tried a lot to figure out what was wrong. When I asked the same in discord, @GaneshS288 told me that it was deprecated from React 19. I hope the note I have drafted above gets approved by a maintainer soon.

Punith1117 avatar Apr 10 '25 09:04 Punith1117

This issue is stale because it has had no activity for the last 30 days.

github-actions[bot] avatar May 11 '25 02:05 github-actions[bot]

@Punith1117 apologies, I missed the discussion here. That looks good to me!

Just a note for the future this kind of back and forth about the specific change that's been proposed is usually done directly in a PR review rather than in an issue, as that makes it a bit easier for us as maintainers to stay on top of things.

wise-king-sullyman avatar May 13 '25 13:05 wise-king-sullyman

@wise-king-sullyman Sorry about that. I am still new to contributing. Thanks for pointing it out! I'll create a PR shortly.

Punith1117 avatar May 16 '25 16:05 Punith1117

This issue is stale because it has had no activity for the last 30 days.

github-actions[bot] avatar Jun 16 '25 02:06 github-actions[bot]