-1

I have react project made with npx create-react-app and the default extension app is.js and it's working same as when I convert it to .JSX . I am new to React and I read JSX is simpler but here both are js and jsx are working same.

devryan
  • 29
  • 6
  • Does this answer your question? https://stackoverflow.com/questions/46169472/reactjs-js-vs-jsx – Daly Sep 21 '21 at 20:30
  • 3
    Does this answer your question? [ReactJS - .JS vs .JSX](https://stackoverflow.com/questions/46169472/reactjs-js-vs-jsx) – Daly Sep 21 '21 at 20:30

1 Answers1

1

You can think of JSX as some kind of Javascript that allows you to put HTML inside it. Normally, When you write a JSX code like this it converted to javascript equivalent under the hood For example

JSX

const Head =  () => (<h1> Hello Wordl </h2>)

Javascript equivalent

const Head = React.createElement('h1', {}, "Hello world")

React.createElement(tagsorComponent, attriborProps, ArraychildrenComponentOrTagsOfStrings)

The first argument can take an element tags or another component as React.createElement

The second arguments takes in the prop of a children component or attributes like ids,

The third argument takes a strings of tags or children component

Imagine you write everything in javascript you may have something that looks like this

const Pet = () => {
  return React.createElement("div", {}, [
    React.createElement("h1", {}, "Luna"),
    React.createElement("h2", {}, "Dog"),
    React.createElement("h2", {}, "Havanese"),
  ]);
};

const App = () => {
  return React.createElement("div", {}, [
    React.createElement("h1", {}, "Adopt Me!"),
    React.createElement(Pet),
    React.createElement(Pet),
    React.createElement(Pet),
  ]);
};

ReactDOM.render(React.createElement(App), document.getElementById("root"));

But with JSX You can simply do it like this

const Pet = (props) => {
  return (
    <div>
      <h1>{props.name}</h1>
      <h2>{props.animal}</h2>
      <h2>{props.breed}</h2>
    </div>
  );
};

const App = () => {
  return (
    <div>
      <h1>Adopt Me!</h1>
      <Pet name="Luna" animal="dog" breed="Havanese" />
      <Pet name="Pepper" animal="bird" breed="Cockatiel" />
      <Pet name="Doink" animal="cat" breed="Mix" />
    </div>
  );
};

render(<App />, document.getElementById("root"));

I think you would prefer the JSX over the raw js equivalent

abubakri olaitan
  • 220
  • 3
  • 11
  • Thank you for your response but my question is that I am writing the same code as you wrote in jsx in my app.js file so why is it working if my app.js has extension js and I am writing jsx syntax in it. – devryan Sep 15 '21 at 03:13
  • 1
    before the app.js get executed it is converted by your babel.js or any transpiler to pure javascript. So that is not an issue. If you try to write JSX inside the browser console it will not work – abubakri olaitan Sep 15 '21 at 03:23
  • 1
    Ab's explanation are solid. I would add that one of the benefits of naming your file to match its syntax more correctly is that it would increase the likelihood of your IDE of parsing its contents in a more meaningful way or providing more meaningful feedback on proper/bad syntax and semantics. For example, if you adopted the ".ts" and ".tsx" suffixes conventions, your IDE would be encouraged to assume type-script support and advise accordingly. – Atmas Sep 15 '21 at 15:01