Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Visual Studio Code to navigate to a React component's source file?

Consider a typical ReactJS file, like:

import Popup from 'components/Popup/Popup';

// ...

<Popup
  trigger={
    <SVG src={pinIcon} className={pinClassName} />
  }
  content={pinTooltipText}
  position="bottom center"
  hideOnScroll
  className="popup-xl--hide"
/>

In VS Code, I want to go to the file that is my component, so jump to components/Popup/Popup. Using Go To Definition:

Go To Definition in VS Code

It sends me up to the import declaration. I can't jump to that file. This is a pain to manage as we have dozens of components and properties moving through them all. Being able to quickly navigate "down" the component stack by going to each definition would be mind-numbingly awesome.

like image 837
David Lozzi Avatar asked Sep 19 '19 15:09

David Lozzi


People also ask

How do you navigate with files in VS Code?

VS Code provides two powerful commands to navigate in and across files with easy-to-use key bindings. Hold Ctrl and press Tab to view a list of all files open in an editor group. To open one of these files, use Tab again to pick the file you want to navigate to, then release Ctrl to open it.

How do I run an imported react project in Visual Studio Code?

Open a terminal on vscode, then make sure you already node installed. Type npm install after that npm run start or whatever command to run, you can see on package. json . Show activity on this post.

How do I open an SRC file in VS Code?

First, select DevTools > Settings > Experiments > Open source files in Visual Studio Code, and then re-start DevTools.

How do I navigate to a component in Visual Studio Code?

It may be needed to restart VS Code, then you should be able to CTRL +click any <Component> or import paths to navigate to it. I was using .jsx files for react component and for me "jsx": "react", option made the difference. If you go up to the listing of the item within the import statement and hit [F12] again, you'll get this window:

How do I open a react app in Visual Studio Code?

To open your React application in VS Code, open another terminal or command prompt window, navigate to the my-app folder and type code .: cd my-app code . In the File Explorer, one file you'll see is the application README.md Markdown file.

How to configure VS Code to recognize all JS files as react?

Configure VS Code to recognize all .js files as React files. Update your settings.json as follows: To access settings.json, simply go to the top menu tab then click View > Command Palette. Type “settings” and then choose the option Preferences: Open Settings (JSON).

How do I launch a website from a react application?

To open your React application in VS Code, open another terminal or command prompt window, navigate to the my-app folder and type code .: ... This will create a launch.json file in a new .vscode folder in your project which includes a configuration to launch the website.


1 Answers

If you have import aliases, through webpack or babel, you can create a jsconfig.json file with the paths property in the compilerOptions.

Here's a Next project's jsconfig.json configured for @/ aliases to the src/ directory.

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "jsx": "react",
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"],
    }
  },
  "exclude": [
    "node_modules",
    "dist",
    ".next",
    ".cache",
    "bundles",
    "out"
  ],
  "include": [
    "pages/**/*",
    "src/**/*",
  ]
}

It may be needed to restart VS Code, then you should be able to CTRL+click any <Component> or import paths to navigate to it.

VS Code component navigation

like image 175
Emile Bergeron Avatar answered Oct 07 '22 20:10

Emile Bergeron