'Difference between Js and Jsx
Got a doubt while I am working on react js. In the below code, I have used both js and jsx. Both responds in the same way. Then why should we use jsx specifically.
When I searched online, I am only able to get the difference between js and jsx but I also need to know whether jsx is built specifically for react js or we can run jsx outside the react js project.
App.js
import logo from './logo.svg';
import './App.css';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'
import Layout from './Components/Layout'
import Home from './Components/Home'
import About from './Components/About'
function App() {
return (
<div className="App">
<Router>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="about" element={<About />} />
</Route>
</Routes>
</Router>
</div>
);
}
export default App;
Home.jsx
export default function Home() {
return (
<div>
Hello, This is home page
</div>
)
}
About.js
export default function About() {
return (
<div>Hello, This is about page</div>
)
}
Layout.jsx
import { Outlet, Link } from "react-router-dom";
export default function Layout() {
return (
<div>
Home | About
<Outlet />
</div>
)
}
Solution 1:[1]
According to React Docs, you can write react code in a .js file as well as.jsx file
In .jsx You are telling your code editor, Hey bro I am writing react code. So it would give you autocompletes and IntelliSense accordingly.
One way to test is to use emmet in both files you won't able to use it in the .js file until and unless you manually change the language type
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Heet Vakharia |
