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:
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.
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.
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.
First, select DevTools > Settings > Experiments > Open source files in Visual Studio Code, and then re-start DevTools.
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:
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With