React is a popular JavaScript library developed by Facebook for building user interfaces. The Visual Studio Code editor supports React.js IntelliSense and code navigation out of the box. Show
Welcome to ReactWe'll be using the
You can now create a new React application by typing:
where 2 is the name of the folder for your application. This may take a few minutes to create the React application and install its dependencies.
Let's quickly run our React application by navigating to the new folder and typing 6 to start the web server and open the application in a browser:
You should see the React logo and a link to "Learn React" on http://localhost:3000 in your browser. We'll leave the web server running while we look at the application with VS Code. To open your React application in VS Code, open another terminal or command prompt window, navigate to the 2 folder and type 8:
Markdown previewIn the File Explorer, one file you'll see is the application 9 Markdown file. This has lots of great information about the application and React in general. A nice way to review the README is by using the VS Code . You can open the preview in either the current editor group (Markdown: Open Preview ⇧⌘V (Windows, Linux Ctrl+Shift+V)) or in a new editor group to the side (Markdown: Open Preview to the Side ⌘K V (Windows, Linux Ctrl+K V)). You'll get nice formatting, hyperlink navigation to headers, and syntax highlighting in code blocks.Syntax highlighting and bracket matchingNow expand the 0 folder and select the 1 file. You'll notice that VS Code has syntax highlighting for the various source code elements and, if you put the cursor on a parenthesis, the matching bracket is also selected.IntelliSenseAs you start typing in 1, you'll see smart suggestions or completions.After you select a suggestion and type 3, you see the types and methods on the object through IntelliSense.VS Code uses the TypeScript language service for its JavaScript code intelligence and it has a feature called (ATA). ATA pulls down the npm Type Declaration files ( 4) for the npm modules referenced in the 5.If you select a method, you'll also get parameter help: Go to Definition, Peek definitionThrough the TypeScript language service, VS Code can also provide type definition information in the editor through Go to Definition (F12) or Peek Definition (⌥F12 (Windows Alt+F12, Linux Ctrl+Shift+F10)). Put the cursor over the 6, right click and select Peek Definition. A will open showing the 6 definition from 8.Press Escape to close the Peek window. Hello WorldLet's update the sample application to "Hello World!". Create a new H1 header with "Hello, world!" and replace the 9 tag in 0 with 1.
Once you save the 1 file, the running instance of the server will update the web page and you'll see "Hello World!" when you refresh your browser.
Debugging ReactTo debug the client side React code, we'll use the built-in JavaScript debugger.
Set a breakpointTo set a breakpoint in 1, click on the gutter to the left of the line numbers. This will set a breakpoint which will be visible as a red circle.Configure the debuggerWe need to initially configure the debugger. To do so, go to the Run and Debug view (⇧⌘D (Windows, Linux Ctrl+Shift+D)) and select the create a launch.json file link to create a 7 debugger configuration file. Choose Web App (Edge) from the Select debugger dropdown list. This will create a 7 file in a new 9 folder in your project which includes a configuration to launch the website.We need to make one change for our example: change the port of the 0 from 1 to 2. Your 7 should look like this:
Ensure that your development server is running ( 6). Then press F5 or the green arrow to launch the debugger and open a new browser instance. The source code where the breakpoint is set runs on startup before the debugger was attached, so we won't hit the breakpoint until we refresh the web page. Refresh the page and you should hit your breakpoint.You can step through your source code (F10), inspect variables such as 1, and see the call stack of the client side React application.For more information about the debugger and its available options, check out our documentation on browser debugging. Live editing and debuggingIf you are using webpack together with your React app, you can have a more efficient workflow by taking advantage of webpack's HMR mechanism which enables you to have live editing and debugging directly from VS Code. You can learn more in this Live edit and debug your React apps directly from VS Code blog post and the webpack Hot Module Replacement documentation. LintingLinters analyze your source code and can warn you about potential problems before you run your application. The JavaScript language services included with VS Code has syntax error checking support by default, which you can see in action in the Problems panel (View > Problems ⇧⌘M (Windows, Linux Ctrl+Shift+M)). Try making a small error in your React source code and you'll see a red squiggle and an error in the Problems panel. Linters can provide more sophisticated analysis, enforcing coding conventions and detecting anti-patterns. A popular JavaScript linter is ESLint. ESLint, when combined with the ESLint VS Code extension, provides a great in-product linting experience. First, install the ESLint command-line tool:
Then install the ESLint extension by going to the Extensions view and typing 'eslint'. Once the ESLint extension is installed and VS Code reloaded, you'll want to create an ESLint configuration file, 6. You can create one using the extension's ESLint: Create ESLint configuration command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).The command will prompt you to answer a series of questions in the Terminal panel. Take the defaults, and it will create a 6 file in your project root that looks something like this:
ESLint will now analyze open files and shows a warning in 1 about 'App' being defined but never used.You can modify the ESLint rules in the 6 file.Let's add an error rule for extra semi-colons:
Now when you mistakenly have multiple semicolons on a line, you'll see an error (red squiggle) in the editor and error entry in the Problems panel. Popular Starter KitsIn this tutorial, we used the VS Code React SampleThis is a sample React application, which creates a simple TODO application and includes the source code for a Node.js Express server. It also shows how to use the Babel ES6 transpiler and then use webpack to bundle the site assets. TypeScript ReactIf you're curious about TypeScript and React, you can also create a TypeScript version of the
See the details at Adding TypeScript on the Create React App site. AngularAngular is another popular web framework. If you'd like to see an example of Angular working with VS Code, check out the Debugging with Angular CLI recipe. It will walk you through creating an Angular application and configuring the 7 file for the JavaScript debugger.Common questionsCan I get IntelliSense within declarative JSX?Yes. For example, if you open the 8 file, you can see IntelliSense within the React JSX in the 5 method.
Bagaimana cara yang benar untuk menggunakan file JavaScript di HTML?Anda bisa langsung menambahkan JavaScript di HTML dengan menggunakan tag <script></script> yang mencakup semua kode JS yang Anda tulis. Kode JS yang bisa ditambahkan: di antara tag <head> di antara <body>
Apa itu getElementById JavaScript?Mengenal Fungsi Document.getElementById() di Javascript
Pada dasarnya fungsi dari Document.getElementById() adalah untuk mengambil sebuah value pada inputan yang ada di HTML tentunya dengan syarat element inputan tersebut memiliki ID , karena fungsi getElementById sesuai namanya, dia akan mengambil value melalui ID.
Apa fungsi dari DOM?Apa Itu DOM? Document Object Model atau disingkat DOM adalah sebuah programming interface (API) untuk web dokumen. DOM merepresentasikan struktur dan konten dari dokumen web dalam bentuk node dan object supaya kita dapat mengubah struktur, style, dan konten dari dokumen tersebut.
Apa yang dimaksud dengan DOM?Document Object Model (DOM) adalah sebuah antarmuka pemrograman (programing interface) untuk HTML, XML dan SVG yang bersifat lintas platform dan language-independent. Sebuah peramban web tidak harus menggunakan DOM untuk menampilkan dokumen HTML.
|