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

feat: Add codemod to transform string refs to arrow-functions #309

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

eps1lon
Copy link

@eps1lon eps1lon commented Sep 25, 2022

For https://github.com/reactjs/rfcs/blob/createlement-rfc/text/0000-create-element-changes.md#deprecate-string-refs-and-remove-production-mode-_owner-field

In: <div ref="refComponent" />;
Out:

<div
  ref={(current) => {
    this.refs['refComponent'] = current;
  }}
/>

Note that this codemod only works for React 18.3 or higher.

Should not be used if the codebase contains warnings about string refs without owners e.g.

Component "ComponentNameWithRef" contains the string ref "someStringRefName". 
Support for string refs will be removed in a future major release.
This case cannot be automatically converted to an arrow function.
We ask you to manually fix this case by using useRef() or createRef() instead.
Learn more about using refs safely here:
https://reactjs.org/link/strict-mode-string-ref

Test plan

  • yarn test
  • node bin/react-codemod.js string-refs ~/repos/react

@eps1lon
Copy link
Author

eps1lon commented Jan 2, 2023

We decided against a codemdoddable warning

@eps1lon eps1lon closed this Jan 2, 2023
@eps1lon eps1lon deleted the feat/string-refs branch January 2, 2023 17:16
@eps1lon eps1lon restored the feat/string-refs branch April 17, 2024 20:38
@eps1lon eps1lon reopened this Apr 17, 2024
@eps1lon
Copy link
Author

eps1lon commented Apr 17, 2024

The plan is to land unfreezing this.refs for 18.3 and 19. Then we can use this codemod.

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

Successfully merging this pull request may close these issues.

2 participants