Live data from Hacker News

Things every React.js beginner should know

camjackson.net

241–247 of 247 posts

Re: Things every React.js beginner should know

#241
post #91

Earlier quoted context omitted.

> ... that then expand to be turing-complete and basically offer everything their primary language offered in the beginning. then they wrote template languages on top of the template language to separate layout and logic again ( PHP vs Twig/Smarty ) so maybe we need to step back a little and figure out how to write a language that wouldn't need a different syntax for logic and layout. This family of languages exists…

I'm a huge fan of Clojure's Hiccup library. It allows the user to make html like the following. Here's a function that always returns true - but it could do any kind of data access. It is just plain Clojure. (defn selected? [] true) Here is a datastructure that represents html: [:h2 {:class (when (selected?) "selected")} "Hello"] Calling the function hiccup.core/html on that datastructure yeilds an html string like t…

I have discovered this little gem, allowing you to do the same in pure Javascript: https://github.com/leeoniya/domvm

The equivalent would be something like

  function() selected { return true }
  ["h2", {class: function() {if (selected()) {return "selected"}}}, "Hello"]
Not as clean, but still usable IMO

Re: Things every React.js beginner should know

#242
post #220

Earlier quoted context omitted.

Exactly the reason I love Rails, even though I'm not a fan of Ruby. Choice paralysis or https://en.wikipedia.org/wiki/Analysis_paralysis It's really severe, but articles from DHH and Rails itself really really help with that. I can't really put enough emphasis how greatly that has helped me.

Sounds like you'd like Ember then.

That's what I thought, tried it. Didn't like it. In the end, it still has to click and having separate templates for logic isn't going to cut it. React does it right, I just wish React came in a package. Which I guess it sorta does now with Redux/Reflux.js and React-Router, etc.

Re: Things every React.js beginner should know

#243
post #216

Earlier quoted context omitted.

Except this isn't about style. It's about logic. Logic isn't as optional as style, and if you separate the logic into one JS file and the DOM into another HTML file, they are still tied together by the JS binding some action to some named DOM element (by selector, etc). The separation that scales is the one that pairs a small piece of DOM (like a piece of text on the page that updates with the current stock price) to…

So this is where you enter a slippery slope from web page to application. Developing a webpage and adding progressive enhancements to HTML elements is a strongly advised best practice with numerous advantages. Developing an dynamic application at the complexity of say Photoshop it would be absurd to have a static HTML file with behavior decorated by JavaScript, or try and offer a fallback. Whilst you could in theory…

You can serve static html and progressive JavaScript and still couple the html and JavaScript into components.

The structure of your code does not have to dictate the result that is served to the user.

Re: Things every React.js beginner should know

#244

Earlier quoted context omitted.

Software is (mostly) about function. Fashion isn't. When something functions on massive scale, it's commendable, and worth looking at.

What does scale have to do with front-end development? Front-end code runs on the user's machine. And regardless, it's folly to make decisions based on wanting your code to scale to Facebook level usage.

I want to create something that works for lots of users from lots of backgrounds and lots of feature complexity.

Re: Things every React.js beginner should know

#245

Earlier quoted context omitted.

The big reason I remember from my PHP days (late 90s early 2000s) is that you want to separate design from development. Graphic designers were expected to know HTML/CSS while developers focus on PHP.

That's never really been true. Graphic designers rarely knew HTML/CSS in the old days.

I have given up on graphic designers that know HTML/CSS. I consider myself lucky if they use some GUI program that outputs clean HTML.

Re: Things every React.js beginner should know

#246
post #96

Earlier quoted context omitted.

This is a debate that has been going on since I started in the late 90ies. You need some sort of logic in your views, no matter what system you use, i. e. loops or formatting dates. Because people are raised on the "don't mix logic & layout" maxim, they feel dirty when they write sich logic in their usual language. .. so they invent template languages... ... that then expand to be turing-complete and basically offer…

Pakyow also uses logicless templates. It achieves this by having processors in ruby that you can bind to scopes/attributes. Now I have everything I need to process this html with my logic to turn it into Frank Bob https://pakyow.org/ edit: I wish downvotes required commenting on why you're downvoting.

Your example actually looks like a namespace disaster about to happen with "id". Needs some prefixing and/or improved naming conventions. I'm not really how making your template have "less logic" than your example would produce a usable template language.

Re: Things every React.js beginner should know

#247

I'm not a React or Angular expert, but do you really put HTML inside of your JS code? It just bothers me too much. We spent years in early web days learning that code and templates should be separate, yet here we are putting HTML inside of code, which goes against years of practice. Can anyone share their professional thoughts about this?

TL;DR: It makes tons of sense.

In the beginning of JS+DOM, it was a common practice to write HTML with DOM events (in JS): (``). Server rendered most things anyway so you could (`)">...">...`). Problem was tons of global functions all over the place, or many big objects and maintainability issues.

In the "jQuery era", you'd write your initial HTML in .html files and jQuery all the events. It brought infinite amount of ways to write code, which often led to many styles and no conventions.

    ">
    ">div
    $('.toggle').click(function () { $($(this).data('target')).toggle() })
or

    +div
    $('.toggle').click(function () { $(this).next().toggle() })
or worse,

    +
    $('.show').click(function () {
       var $this = $(this)
       var $div = $('').text($this.data('text'))
       $this.after($div)
    })
etc.

Many problems with this approach:

- You can't tell which element has events bound to it just by looking at it. Any JS file can decide to do that - Usage of a CSS class to find elements is just a hack - Poorly named IDs/classes create collisions - After an action, the initial HTML no longer represents the current state - Server can render some HTML, then client changes it, and if things get out of sync, the only way to reset the state is to reload the entire page. Imagine a user signing in, and many parts of the page need to show things for a signed in user. Doing this manually would probably be error-prone, so it's just easier to reload the page and let the server do that.

And then came the modern front end libraries/frameworks like React, Angular, Ember etc.

The main difference is that now the HTML representation is ALWAYS in sync with the data, AND the functional code that's attached to each element is at the same place.

And the jQuery example becomes this:

    // react

    +
    {this.state.isVisible ? div : null}

    toggle() { this.setState({ isVisible: !this.state.isVisible }) }


    // angular 1.x

    +
    div

    scope.toggle = () => scope.isVisible = !scope.isVisible

When you look at this piece of code you immediately understand the ways it can be rendered and what affects it. Super clear, no surprises.
Post reply on HN