3

EDITED

Excuse me for the very generic title, but as this is my first attempt to use React, I really have no idea of what I'm doing.

So, I'm using Symfony Encore to use React in my Symfony app.

I've installed all the required dependencies:

npm install babel-preset-react --save
npm install react --save
npm install react-dom --save
npm install prop-types --save

Then I wrote my first React script:

import React from 'react';
import ReactDOM from 'react-dom';

export default class HelloWorld extends React.Component {
    render() {
        return (
            <div>
                Hello World!
            </div>
        )
    }
}

ReactDOM.render(<HelloWorld />, document.getElementById('todo-test'));

In a first attempt I tried to follow this tutorial where is used var HelloWorld = React.createClass({... but it never worked as I received the error

ReactJs CreateClass is not a function.

After some searches, I came to this Stackoverflow answer where is suggested to use import instead of var.

In other answers is suggested also to use module.exports.

Now, as this is really the first time I approach React and Javascript, my question is:

Given the above code, what I wrote?

Is it Javascript? Is it TypeScript? Is it ES6?

I'm a bit confused by all the acronyms and sub-languages and now I don't know what I wrote.

I know it works, but I don't know the internals and I'm trying to understand them...

Do anyone can help me to better understand the code above? Any references on Internet?

Thank you.

EDIT

Following the comment of @Cerad in the comments, I expand my question.

As you can see, the class I posted (and that works!) is different than the one in HelloWorld example from the React documentation:

ReactDOM.render(
  <h1>Hello, world!</h1>,
  document.getElementById('root')
);

This last one, uses ReactDOM.render instead of

export default class HelloWorld extends React.Component {
    render() {
    ...

Which is the difference?

More, this "official" syntax is also different from the one I found in the linked tutorial that is this:

var HelloWorld = React.createClass({
  render: function(){
    return (
      <div>
        Hello World!
      </div>
    )
  }
});


ReactDOM.render(<HelloWorld />, document.getElementById('app'));

So I have actually 3 different syntaxes to choose from!

Which one should have I to choose? How I do a choice?

In the end, the compiled version of the three syntaxes is the same?

Where can I find the compiled version? Is it useful to read it?

Aerendir
  • 6,152
  • 9
  • 55
  • 108
  • The HelloWorld class you posted is written in Javascript ES6. The template portion
    Hello
    is written in a react specific language called JSX. Eventually, all those processors and what not end up generating plain old es5 javascript for the browser. Fun stuff. Instead of starting with Symfony and Encore, you might want to get a bit comfortable with react using https://reactjs.org/docs/hello-world.html
    – Cerad Oct 28 '17 at 14:18
  • @Cerad thank you for your comment. I've updated the question with more details... I'm even more confused o.O The theory is clear... Is the prcatice that is an Hell of alternatives! – Aerendir Oct 28 '17 at 14:33
  • @Aerendir I've started using React myself And one thing I can say for sure is that React offers a lot of ways to do something. You have to find an option where you feel most comfortable with. Anyone I spoke with, about React, advices me to use JSX as much as possible. Also, it's good to learn first Javascript ES6 as this will help you a lot. – Giesburts Oct 28 '17 at 15:10
  • @Gijsberts, ok, I suspected that JSX is a great thing to use and also all tutorials I read suggest to use it as much as possible. So this is clear for me... Also the great number of ways to do something is now clear to me... The problem is understand why something works and something don't. – Aerendir Oct 28 '17 at 15:14
  • I'm coming from the `PHP` world where if something is wrong a clear error of some kind is printed in bug letters on the screen and there is a stack trace where you can find the origin of the error, fix it and live happy. In Javascript I'm not so comfortable with the console and my life is becoming a bit complex -.-' – Aerendir Oct 28 '17 at 15:15
  • 1
    The more you will use Javascript (And learn obvs) the more you will recognize the errors. Maybe it's good to learn to fundamentals of Javascript first and after that ES6 and for instance React. What helped me is to use the `create-react-app` CLI to mess around with. – Giesburts Oct 28 '17 at 15:41
  • @Gijsberts thank you for the advice! I'll give it a try! – Aerendir Oct 28 '17 at 15:42
  • 1
    Consider the reactjs site to be the gold standard. It's written and maintained by the actual react developers. The Javascript eco-system is in the midst of massive changes. Any 3rd party tutorial should be considered suspect unless proven otherwise. And yes, create-react-app is a good starting point though online tools such as codepen let you experiment without installing anything on your machine. – Cerad Oct 28 '17 at 17:08

0 Answers0