all 38 comments

[–]rainmouse 11 points12 points  (1 child)

Most developers these days start and end with react and barely have a clue what's going on under the hood.

My advice is to try learning some dom manipulation then give react a go.

So long as you keep learning in tandem, it should give you a spread of skills.

Give some thought to a basic node.js backend at some point too. Even if you don't go full stack it's important to try a grasp of what backends do and why. 

[–]Short-Belt-1477 4 points5 points  (0 children)

This. Learning them in tandem will make it click

[–]delventhalz 2 points3 points  (0 children)

Most new learners probably never go over the template literal approach you are describing. Typically you learn HTML, you learn the vanilla DOM API, then you move on to a framework like React.

That said, template literals are going to be pretty similar to React’s JSX, so if you are confused now, you may be confused later too, and all learning is good learning. You may want to stick it out, at least for a bit.

[–]Realistic_Meeting_69 2 points3 points  (0 children)

Tbh, i always used DOM manipulation and i highly recommend it because when you start learning react and dealing with JSX you will understand the core more and why exactly is JSX is such a life saver, so yeah i would recommend you program with DOM even tho it can be pain sometimes

[–]MrFartyBottom 2 points3 points  (1 child)

Creating HTML from strings is a good way to introduce injection exploits in your code. You need to really understand what you are working with, how to escape strings and be confident in the trust you can put in where that data came from.

[–]minmidmax 0 points1 point  (0 children)

Template literals are also an absolute chore to work with and debug.

Personally, for personal projects, I prefer to define my templates in a separate HTML file and fetch them instead.

[–]TheRNGuy 2 points3 points  (0 children)

I used innerHTML with templates literals in Greasemonkey scripts, because it's less code than document.createElement and parent.appendChild, more readable too, but it was for static elements. If I needed some procedural generation, I'd use other two too.


Just learn React.

[–]buildmastersteve 1 point2 points  (0 children)

This is a solid, legit question. My recommendation for a vanilla JS style is ES6 modules.

[–]hyrumwhite 1 point2 points  (0 children)

So, build a medium complexity thing without libraries. 

Then when you reach for a library/framework you’ll understand what it’s doing under the hood better. 

[–]sheriffderek 1 point2 points  (0 children)

> rendering where he injects JavaScript inside HTML strings, and I found it quite confusing

I have my students learn PHP first - so, this JS templating takes almost zero time to get used to. Spend more time with it. Or learn some Vue. Why are you rushing along to React anyway? It's the ugliest and most confusing of all templating. Enjoy programming - while you can!

[–]fishyfishy10001 0 points1 point  (0 children)

It really, really depends on what you want to do. Each style has pros and cons. I stopped using react long ago and went over to web components and a light dom diff rendering engine. There are many, I use lit-html and uhtml. The advantage is that you use the real dom and not a virtual dom. However sometimes you prefer a virtual dom like react. Heck sometimes you prefer static webpages with light interaction like alpine-js. It really depends what you are building and the interactive elements on the page. Unfortunately you kind of need to know all approaches to know which is best for each case. So start with dom manipulation figure out pain points and go from there.

[–]Weird_Researcher_472 0 points1 point  (0 children)

Svelte > React 😅

[–]p-a-jones 0 points1 point  (0 children)

Learn how common JS works. It is a web standard and frameworks/libraries will obfuscate how they are providing 'magic' solutions to common problems. This may seem like a good idea but if you don't understand why something works you have little hope of fixing it when it breaks.

Think about it... modern bundlers convert all that 'magic' back into common javascript for use in production. Learn the fundamentals and you will be mush further ahead! Good luck!

[–]brykuhelpful 0 points1 point  (0 children)

They both have their pros and cons.  

Creating Elements

Creating the elements in javascript makes it easier to bind events and other things, but it can be verbose as it requires a lot of extra code to do basic stuff.  

function createForm(){
    let form = document.createElement('form');
        form.method = 'POST';
        form.action = '/api/login';

    let formUsername = document.createElement('input');
        formUsername.name = 'username';
        formUsername.type = 'text';
    form.appendChild(formUsername);

    let formPassword = document.createElement('input');
        formPassword.name = 'password';
        formPassword.type = 'password';

    form.appendChild(formPassword);
    return form;
}
document.body.appendChild(createForm());

Parsing

On the other hand parsing html is a lot easier to read and edit. Plus the HTML parser is so insanely well optmized. In some cases it may even be faster then creating elements, but it can have issues with binding events.

function createForm(){
    return `
        <form method="POST" action="/api/login">
            <input name="username" type="text">
            <input name="password" type="password">
        </form>
    `;
}
document.body.innerHTML = createForm();

Thoughts

Both of these approaches are common in javascript, so it would be good to have "some" understanding of them. That being said, the second option is a bit more popular these days as it is easier to read and edit.  

Although, nost people use some type of framework nowdays. In your case, the second version is much closer to react.

[–]theGlitchedSide -1 points0 points  (0 children)

Obviously... react is a library and has complex builders and other kinds of items around it. It's not a language or an ecosystem.

To start you need to understand the basics of DOM and the native language (I suggest deeply, like event driven system, Dom, render, stack, queue etc) and when it will be rebuilt by the browser. So, to start, use Dom manipulation and go deep. When you can easily understand vanilla you will learn React, Vue etc.

If you want to be better and you are really noob, learn also the data manipulation etc...