What's so cool about React? Am I obsoleting myself by not learning it?

This is totally normal. I think the 5 stages of grief apply here :laughing: Once you start to get used to the look it tends to look very neat and orderly. Oddly enough I now feel that my older apps with templates/javascript helpers look/feel like a mess.

So actually if anything itā€™s using a markup (JSX) in JavaScript not the other way around. The React component is all JS except for whatā€™s returned in the render method. You can use all of JS above the return to add as much logic as needed.

JSX is just a convenience for designers and for developers because itā€™s easier to think about markup than function calls. This is an example from my react login buttons project that doesnā€™t use JSX (iā€™m doing this because so people might not have JSX installed).

So personally I wouldnā€™t quite write it that way in production. Itā€™s shorter but less clear whats happening at glance. The component will re-render every time setState is called so here, every time text is entered in the textarea. On this re-render pass itā€™s checking to see if length is truthy and returns a boolean. If itā€™s > 1 then it will be visible.

Worth noting, the entire render block will be rendered, in Blaze only the bit that is reactive will change if itā€™s data changed.

Hereā€™s a slightly more readable version. notice that this.isDisabled is getting called right away as opposed to the this.handleChange which is just the func. reference. Youā€™re looking at the render method and can now declaratively setup how it will look at any state, as opposed to imperatively setting it up with PHP.

var TweetBox = React.createClass({
  getInitialState() {
    return { text: "" };
  },

  handleChange(event) {
    this.setState({ text: event.target.value });
  },

  isDisabled() {
    return this.state.text.length === 0;
  },

  render() {
    return (
      <div>
        <textarea onChange={this.handleChange}></textarea>
        <button disabled={this.isDisabled()}>Tweet</button>
      </div>
    );
  }
});

Itā€™s used frequently. Using this.setState in larger apps is only used if no other component wants to know about the state. If it does itā€™s either pushed up higher so both can receive that state or something like flux is used to globally share it in a sane way.

Hereā€™s an example of declaring if a user should see the ā€˜delete postā€™ button.

This explains it far better than I could, itā€™s even queued up to answer why FB does this :wink: https://youtu.be/DgVS-zXgMTk?t=113

I though this as well at first. However this is because I was making large templates. I donā€™t know what the product card looks like but it could prob. be broken down into 4-5+ components, stored in a product_card/ folder. You can also render a blaze template right into the JSX so itā€™s possible to do that to.

Hehe, yea this is prob. half and half fault. They have a method called componentDidMount that fires after the itā€™s inserted into the DOM and I had that in my head. I just meant that it needs to be inserted into the DOM. :smile:

Itā€™s an optional package that comes with the Meteor react package by default. Itā€™s made by MDG. Itā€™s in the Meteor tutorial I linked to :wink:


[quote="thebionicman, post:38, topic:8100"] I haven't had a chance yet to read beyond the first page of the overview on the Flux site. It said that it is more of a process than a framework, though. Is it like a set of guidelines? Or does it involve some sort of API at all? [/quote]

Ah yea donā€™t let flux ruin the simplicity of React :slight_smile: So flux is just a spec they released on how you can do this. They do provide a dispatcher but nothing else. This led to a flurry of flux libraries that do the same thing, but with different APIs. My favorite is Alt.

Itā€™s pretty much the wild west. Most stick to the pattern. Some like Reflux remove constants and the dispatcher which breaks the point of flux. Alt will hide these as implementation details and instead uses some convention to generate them for you, providing a nice simple API and still sticking to the pattern.


The gist is that it allows you to decouple your view and share state across the app in a sane way.
  • View calls an action (w/ optional payload) and itā€™s job is done
  • Action method takes payload and could modify it and then dispatches it
  • Data store (state obj) handles action and can use payload to update itself and fires ā€˜changedā€™ event
  • Views receive changed message and re-render

If youā€™re interested in how this works this is a great article.

1 Like

Iā€™m beginning to see how Reactā€™s philosophy of componentizing everything makes sense, at least for more complex front-end apps. Youā€™d probably quickly see diminishing returns for your effort the smaller your apps became, but it does sound like a good tool for the big ones.

1 Like

Yea itā€™s funny how it changes the way you think about building an app. Before long youā€™ll look at a UI and sayā€¦ oh thatā€™s a component, thatā€™s a component, and then even try to think about abstracting things out to make it re-useable in any app :smile:

I find that even small apps have this benefit.

Hereā€™s an example of a tiny app with 5 components. It could easily be 1 template, however by making several 15-30 line components itā€™s super easy to isolate them in your head based on their role:

1 Like

@SkinnyGeek1010
So is that it then: the paradigm shift React brings is the division of the UI into a Component model?

Donā€™t get me wrong - itā€™s a really cool philosophy, and Iā€™m just trying to understand what Reactā€™s differentiating factor is. But does my understanding of it in that way sound accurate? I wonder because it doesnā€™t sound so radcally new - I mean, Angular and Ember introduced the concept of modeling the UI as a hierarchy of objects representing DOM elements (well, Iā€™m not sure if the Angular and Ember models are actually hierarchical, technically) - even Backbone did that to a much lesser degree, right?

Thanks everyone for your replies. I got a large amount and canā€™t really reply to them all but I really appreciate the time taken to explain this stuff.

Youā€™ve at least removed some of the barriers to entry that React seems to have. Although i still think JSX looks messy for now :smile:

Iā€™ll play around with some of your examples and if anymore open source meteor react projects iā€™ll be sure to check them out. Until then I think iā€™ll stick to Blaze.

1 Like

Yep you got it! I think React takes this further than Angular 1.x and Ember 1.x. The newest versions of those are more similar to React.

React ideally wants to make it so that one component is completely self sufficient. You could even use inline CSS but that may boarding the crazy train :smile:

Awesome, I donā€™t think I linked all my React/Meteor stuff but checkout my github for all of them: https://github.com/AdamBrodzinski I also have some tables coming up that uses a combo of Blaze and React.

Sounds like a plan! btw I still think Blaze is awesome! Itā€™s a huge improvement over handlebars.

1 Like

Thank you for your patience and detailed explanations

1 Like

Wow, @SkinnyGeek1010 wins helpful user of the week award! :wink: Thanks for putting so much time into your replies.

I think Iā€™m sold on React. Iā€™m a little fuzzy on a few things, like the concept of server-side rendering (never heard of this term before now). I definitely have some readingā€”and tutorialsā€”to dive into later!

4 Likes

Wow, @SkinnyGeek1010 wins helpful user of the week award! wink Thanks for putting so much time into your replies.

You know what, this thread and his replies have finally allowed me to decide giving react a go in a project and the more I skimmed through those links he provided, the more it grew on me.

Iā€™m now excited to finish the one Iā€™m working on and take on some brand new react challenge :smile:

4 Likes

Yeah man! This is what the developer community is all about. :slight_smile: Turning our peers onto new ways of doing things, so that we all learn and grow and become even better (and faster?) at building things.

I got totally burnt out on web development several years ago, because it was feeling stagnant to me. Now thatā€™s changed, and I feel like itā€™s more exciting to be a web developer than it ever has before.

3 Likes

I fully agree :smile:
But concerning the new ReactiveVar, its simpler to use my https://atmospherejs.com/frozeman/template-var
I donā€™t get why its not more popular, because this makes handling states inside an template instance a breeze and its a standard package in all of my projects.

(It also allows you to get the reactive var of another template)

4 Likes

I have to disagree with this one.

If you write you meteor templates/components smartly and use template instance specific reactive vars (like template-var), you can very well write modules, where simple pass reactive data in and get a nice component, which doesnā€™t pollute you global apps namespace or variables at all.

I totally agree with you. I think I should have said something more like, ā€œBlaze lets you make a mess. Itā€™s totally up to the developer to write good modular code.ā€

React and itā€™s ecosystem is concerned about building scalable (as in team/code size) maintainable apps. This concern trickles down into the docs and APIs. The core team developed these to solve their own problems.

I think this would be a much better situation if MDG had a very clear suggested architecture, methodology, style guidesā€¦ eg.

// bad
$('body').hasClass('extra-large-fonts')
// good
appState.get("hasLargeFonts");

most examples in the open use a mix of imperative and declarative paradigms where declarative would work better, and storing state in the DOM instead of a Reactive Dict or Var. Thereā€™s also no suggestion to component-ize things so a lot of code is like a static HTML page with duplicated (or global) helpers used across each page (instead of making re-usable Blaze components).

Hereā€™s a decent example of using Blaze in a modular way similar to React (with the exception of how it renders).

Also Template vars are something recent. Itā€™s unfortunate that theyā€™re optional and feel ā€˜bolted onā€™ instead of being first class. Over the years this transient state was stuffed into a global Session (oh man I feel like a cranky old timer now :laughing:).


> That's JQuery vs React, let's compare Blaze vs React:...

If I can find enough time I would really like to do this. I think the differences are not obvious as most people compare speed and how much ā€˜more complexā€™ it initially looks compared to handlebars.

The big differences are in code maintenance, debugg-ability, test-ability, and an easier to reason mental model of how data flow throughout the app.

These arenā€™t obvious until you have the chance to work on an app with tens of thousands of line and many months of half-thought through product changes. Once youā€™re in this spot itā€™s much easier to see the differences. Small todo apps are always easier in Blaze 10/10 times.

After writing a lot of React code itā€™s made my Blaze code 10x better. However itā€™s hard to go back for new projects because itā€™s solved a lot of issues for me (I still write Blaze though!)

As with the link aboveā€¦ you ā€˜canā€™ write Blaze like React but no-one really does (in the open).

just my $0.02 :smile:

1 Like

I did not see any mention of SSR. For me, itā€™s a killing feature that could justify the use of React. (or angular 2.0)

2 Likes

I think this covers the only real pain point with using Blaze. I sometimes avoid creating sub-templates only because child templates canā€™t inherit reactive helpers (so far as I can tell). Is there any discussion/roadmap to address this?

2 Likes

@frozeman that template-var package is exactly what Iā€™ve been looking for :+1: do you typically grab data from subscriptions for a template and store them in templateVars as well to keep all template state in templateVars, or just data thatā€™s not from a subscription? Iā€™m deciding between Blaze and React and although I really like Blazeā€™s simplicity and syntax I love how React maintains state for each component. Iā€™d prefer to explicitly state each Blaze templateā€™s state variables rather than assume everything required is present from a subscription

Just wanted to provide an update:

Iā€™m knee-deep in React and building a new SaaS using it. Thereā€™ve been a few growing pains, but I really like it! Too bad there arenā€™t more tutorials out there for Meteor+React.

2 Likes

I mainly use it to keep the template instance specific state there, e.g. what box is ticked, or slided out etc.
Glad that you like it!

Wouldnā€™t you just handle the radio button change with a function in the React component that used setState on a variable that could be used to hide or show a form?

This might not be a very important concern for people reading meteor forums, but: React is more ubiquitous. You can use it with Meteor. And with Rails. And with .NET. And with any other backend. Whereas with Blazeā€¦ I hadnā€™t even heard about Blaze before trying Meteor, and I try my best to keep up with the industry.

This also has a benefit of more experience being out there. For any given problem, there have been more people solving it with React than Blaze. More people have given thought to structure and best practices. And maybe even released the solution as a library you can re-use.

And in the end, if Meteor does not work out for you, itā€™s easier to migrate a React frontend to something else.

2 Likes