Skip to content

Rename React's SFC to 'FunctionalComponent' #30364

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

Merged
merged 5 commits into from
Nov 10, 2018
Merged

Rename React's SFC to 'FunctionalComponent' #30364

merged 5 commits into from
Nov 10, 2018

Conversation

rhysforyou
Copy link
Contributor

Note: I'm reopening this as a new PR since #30188 was automatically closed while waiting for fixes to other React tests to merge.


This PR renames React.SFC and React.StatelessComponent to React.FunctionComponent, while introducing deprecated aliases for the old names. The motivation for this is twofold: firstly the addition of hooks means function components can have state, and secondly the term "stateless component" can equally apply to class components which don't use any state, causing some confusion.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

If changing an existing definition:

Sorry, something went wrong.

@typescript-bot typescript-bot added Popular package This PR affects a popular package (as counted by NPM download counts). The Travis CI build failed labels Nov 8, 2018
@typescript-bot
Copy link
Contributor

typescript-bot commented Nov 8, 2018

@rhysforyou Thank you for submitting this PR!

🔔 @johnnyreilly @bbenezech @pzavolinsky @digiguru @ericanderson @morcerf @tkrotoff @DovydasNavickas @onigoetz @theruther4d @guilhermehubner @ferdaber @jrakotoharisoa @pascaloliv @Hotell @franklixuefei @icopp - please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

@typescript-bot
Copy link
Contributor

typescript-bot commented Nov 8, 2018

@rhysforyou The Travis CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

@danielkcz
Copy link
Contributor

@rhysforyou The PR ##30331 fixing all React tests failing has been merged so I recommend rebasing and it should be much easier to make tests passing here.

Also can you please add/change alias SFC to FC? I don't really like such a long name as FunctionComponent everywhere.

@rhysforyou
Copy link
Contributor Author

I've rebased and added React.FC<P> as an alias to React.FunctionComponent<P>, let's see if this passes.

@typescript-bot typescript-bot added Awaiting reviewer feedback Has Merge Conflict This PR can't be merged because it has a merge conflict. The author needs to update it. and removed The Travis CI build failed labels Nov 8, 2018
@typescript-bot
Copy link
Contributor

@rhysforyou Unfortunately, this pull request currently has a merge conflict 😥. Please update your PR branch to be up-to-date with respect to master. Have a nice day!

@rhysforyou
Copy link
Contributor Author

All fixed up 😄

@typescript-bot typescript-bot removed the Has Merge Conflict This PR can't be merged because it has a merge conflict. The author needs to update it. label Nov 9, 2018
@johnnyreilly
Copy link
Member

johnnyreilly commented Nov 9, 2018

Thanks - could you add a test for back compatibility please? I'd like to see evidence that works as otherwise this will be a very breaking change to the type definitions. Essentially all the tests that exist for StatelessComponent should stay in existence and pass as well as the new tests for FunctionComponent.

@rhysforyou
Copy link
Contributor Author

@johnnyreilly done, I've added back tests that deal with the SFC type to ensure that those aliases work properly.

@johnnyreilly
Copy link
Member

Looks good to me. @kovensky - does this look good to you?

@Jessidhia
Copy link
Member

Looks good to me (can't use review UI as I'm on phone atm).

I see you also accidentally(?) ran Prettier on the code ;)

@rhysforyou
Copy link
Contributor Author

Oops, I’ll fix that up haha.

Revert "remove use of deprecated APIs"

This reverts commit 6bdf2ed.

remove use of deprecated APIs without reformatting
@rhysforyou
Copy link
Contributor Author

rhysforyou commented Nov 10, 2018

Nothing a little interactive rebasing couldn't fix. Assuming I didn't break anything between all those squashes and reverts, and this PR goes green, it should be ready to merge.

@johnnyreilly johnnyreilly merged commit ab11f1b into DefinitelyTyped:master Nov 10, 2018
@johnnyreilly
Copy link
Member

Thanks

@amanmahajan7
Copy link
Contributor

Is there a reason for releasing this change in a patch version? It is a breaking change and should probably have been released in a major version.

@danielkcz
Copy link
Contributor

danielkcz commented Nov 21, 2018

@amanmahajan7 What's breaking change? Old stuff is just deprecated, but it's still there. You have plenty of time for updating.

@amanmahajan7
Copy link
Contributor

Sorry I wasn't clear. May be it is a specific case and does not need a major bump but here is an example.

If the tsc compiler is generating definition files then after the upgrade the compiler generates files with React.FunctionComponent instead of React.SFC and if the consumer package is still using an older version of @types/react then it won't be able to consume those generated files

@johnnyreilly
Copy link
Member

I'm afraid all type definition changes are potentially breaking - there is no semver. Details here:

https://blog.johnnyreilly.com/2018/09/ivan-drago-and-definitely-typed.html

@stramel stramel mentioned this pull request Nov 17, 2020
Dogyunjeong added a commit to Dogyunjeong/simple-react-snippets that referenced this pull request Jan 10, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Due to the deprecation of the Stateless function component.
ref: DefinitelyTyped/DefinitelyTyped#30364
hrrytech92 added a commit to hrrytech92/react-redux-typescript-guide that referenced this pull request May 26, 2022
📖Update to use FC and not SFC
DefinitelyTyped/DefinitelyTyped#30364

By  @Pau1fitz:
* Initial work

By @piotrwitek 
* rebase on master
* Refactored all references related to SFC, stateless & stateful in favour of function and class components
* Other minor improvements
hrrytech92 added a commit to hrrytech92/react-redux-typescript-guide that referenced this pull request May 26, 2022
* 📖Update to use FC and not SFC

DefinitelyTyped/DefinitelyTyped#30364

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Refactored all previous references related to SFC, stateless & stateful in favour of function and class components

Other minor improvements
ThomasRoest pushed a commit to ThomasRoest/react-styleguidist that referenced this pull request Feb 9, 2023
ThomasRoest added a commit to styleguidist/react-styleguidist that referenced this pull request Feb 10, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
* chore: upgrade react dependencies

* chore: replace ReactDOM.render with createRoot in index.ts

* chore: fix new TS errors after upgrading @types

* chore: replace ReactDOM.render in Preview.tsx

* chore: replace ReactDOM.unmountComponentAtNode with root.unmount()

* chore: remove ReactDOM import

* chore: upgrade react testing library

* chore: replace deprecated React.SFC type with React.FC

DefinitelyTyped/DefinitelyTyped#30364

* chore: Fix TS errors in propTypes.children

propTypes.node results in TS errors. There doesn't seem to be a good alternative ( other than making the children propTypes more strict, which could result more errors)
Instead use something less restrictive (propTypes.any) for children and components.

* chore: fix TS errors, extract props interfaces with children

* chore: fix TS errors, add context types

* chore: TS fix

* fix: Fix React unmount error

Unmount asynchronously with setTimout
https://stackoverflow.com/questions/73459382/react-18-async-way-to-unmount-root

error message:
Warning: Attempted to synchronously unmount a root while React was already rendering. React cannot finish unmounting the root until the current render has completed, which may lead to a race condition.

* test: make wrapper unmount test async

---------

Co-authored-by: Thomas Roest <thomas.roest@moxio.com>
ThomasRoest added a commit to styleguidist/react-styleguidist that referenced this pull request Feb 10, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Upgrade to React 18

* replace ReactDOM.render with createRoot in index.ts
* fix new TS errors after upgrading @types
* replace ReactDOM.render in Preview.tsx
* replace ReactDOM.unmountComponentAtNode with root.unmount()
* remove ReactDOM import
* upgrade react testing library
* replace deprecated React.SFC type with React.FC

DefinitelyTyped/DefinitelyTyped#30364

* Fix TS errors in propTypes.children

propTypes.node results in TS errors. There doesn't seem to be a good alternative ( other than making the children propTypes more strict, which could result more errors)
Instead use something less restrictive (propTypes.any) for children and components.

* fix TS errors, extract props interfaces with children
* fix TS errors, add context types
* TS fix
* Fix React unmount error

Unmount asynchronously with setTimout
https://stackoverflow.com/questions/73459382/react-18-async-way-to-unmount-root

error message:
Warning: Attempted to synchronously unmount a root while React was already rendering. React cannot finish unmounting the root until the current render has completed, which may lead to a race condition.

* make wrapper unmount test async

---------

Co-authored-by: Thomas Roest <thomas.roest@moxio.com>
bumpedyou pushed a commit to bumpedyou/react-redux-typescript-guide that referenced this pull request Apr 19, 2023
📖Update to use FC and not SFC
DefinitelyTyped/DefinitelyTyped#30364

By  @Pau1fitz:
* Initial work

By @piotrwitek 
* rebase on master
* Refactored all references related to SFC, stateless & stateful in favour of function and class components
* Other minor improvements
bumpedyou pushed a commit to bumpedyou/react-redux-typescript-guide that referenced this pull request Apr 19, 2023
* 📖Update to use FC and not SFC

DefinitelyTyped/DefinitelyTyped#30364

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Refactored all previous references related to SFC, stateless & stateful in favour of function and class components

Other minor improvements
Codechief09 pushed a commit to Codechief09/react-n that referenced this pull request Oct 9, 2023
Use the term "function" instead of "functional" when referring to React function components.

When hooks were introduced, SFC (Stateless Functional Component) was dropped in favor of FC (Function Component). With hooks, function components are no longer restricted to being stateless. The term "function" was adopted instead of "functional" since the latter implies Functional Programming paradigms such as pure functions, which is not a requirement of a function component.

https://twitter.com/dan_abramov/status/1057625147216220162
DefinitelyTyped/DefinitelyTyped#30364
Codechief09 pushed a commit to Codechief09/react-redux-typescript that referenced this pull request Oct 9, 2023
📖Update to use FC and not SFC
DefinitelyTyped/DefinitelyTyped#30364

By  @Pau1fitz:
* Initial work

By @piotrwitek 
* rebase on master
* Refactored all references related to SFC, stateless & stateful in favour of function and class components
* Other minor improvements
Codechief09 pushed a commit to Codechief09/react-redux-typescript that referenced this pull request Oct 9, 2023
* 📖Update to use FC and not SFC

DefinitelyTyped/DefinitelyTyped#30364

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Refactored all previous references related to SFC, stateless & stateful in favour of function and class components

Other minor improvements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Popular package This PR affects a popular package (as counted by NPM download counts).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants