1

I have some component like this

const Component = ({ oldPropName }) => ...

which I want to refactor to:

const Component = ({ newPropName }) => ...

and I want all occurrences of this oldPropName across all files to be changed automatically. Is there way to do this with VSCode?

Essentially same question can be asked as how to rename a destructured function parameter across the files without thinking about React components.

wunnle
  • 39
  • 1
  • 5
  • Why does search-and-replace not work for you here? – jmargolisvt Nov 07 '21 at 16:30
  • Does this answer your question? [How do I find and replace all occurrences (in all files) in Visual Studio Code?](https://stackoverflow.com/questions/37346481/how-do-i-find-and-replace-all-occurrences-in-all-files-in-visual-studio-code) – Randy Casburn Nov 07 '21 at 16:32
  • @jmargolisvt It doesn not because `oldPropName` may be a common prop name like `onClick`. Search and replace will accidentally change prop names of irrelevant components. – wunnle Nov 07 '21 at 16:37
  • @RandyCasburn Unfortunately not. See the comment above – wunnle Nov 07 '21 at 16:38
  • According to your question, though, there are no irrelevant components. You want to make this change _across all components_. – Andy Nov 07 '21 at 17:08
  • Two components can have a prop called onClick. I wouldn't want to do a mass find and replace for some generic prop name like onClick. – wunnle Nov 07 '21 at 18:20

2 Answers2

1

F2 will change all variable occurrences in the code if you disable the following settings:

vs code settings screen

Grim
  • 45
  • 5
0

You can right click the oldPropName and there should be a "Rename Symbol" option or you can use F2. Some languages support rename symbol across files. Press F2 and then type the new desired name and press Enter. All usages of the symbol will be renamed, across files.

Stoobish
  • 1,202
  • 4
  • 23
  • F2 or "Rename Symbol" just assigns a name to destructured variable like the following: ```const Component = ({ oldPropName: newPropName }) => ...``` – wunnle Nov 07 '21 at 16:36