News for hackers. Stuff that matters.
Re:Redux – Not Dead Yet by mephitix on Tuesday April 24, 2018 @10:39PM (#16918289)
How is tag-nesting not considered an API for React? The entire point of the new context API is to get around prop-drilling. Being able to do that and express the intent in JSX is what it does well IMO...
Re:Redux – Not Dead Yet by hestefisk on Tuesday April 24, 2018 @11:07PM (#16918372)
Context to me seems like a sophisticated way of declaring global variables. Might as well do const GLOBALS = { ... } instead of another complex API that has to be loaded.
Re:Redux – Not Dead Yet by eropple on Wednesday April 25, 2018 @12:26AM (#16918620)
It's not, though, any more than dependency injection is "declaring global variables".
Re:Redux – Not Dead Yet by crooked-v on Wednesday April 25, 2018 @01:14AM (#16918780)
The new context API effectively makes variables available globally, but you're still using a separate (explicitly declared) Consumer for each, so you don't have any of the basic issues that go with using a single set of globals.

For a very simple example:

    import { UserIdConsumer } from '@/contexts'

    const ThisComponent = () => (
      <div>
        User ID: <UserIdConsumer>{value => value}</UserIdConsumer>
      </div>
    )

    export default ThisComponent
Re:Redux – Not Dead Yet by hestefisk on Wednesday April 25, 2018 @04:19AM (#16919523)
Not sure I follow. Where does value come from?
Re:Redux – Not Dead Yet by tym0 on Wednesday April 25, 2018 @08:53AM (#16921042)
UserIdConsumer will call the children prop as function, UserIdConsumer could look like this:

    const UserIdConsumer props => <Fragment>props.children(theValueYouWantAvailable)</Fragment>
Re:Redux – Not Dead Yet by tootie on Tuesday April 24, 2018 @11:25PM (#16918447)
That's Stockholm Syndrome from staring at too much JSX. We abandoned programming with XML in 1999 for a reason. JSX is acceptable as fancy markup, not with magic behavior.
Re:Redux – Not Dead Yet by dmitriid on Wednesday April 25, 2018 @12:34AM (#16918654)
JSX is a paper-thin XML-like DSL on top of regular Javascript.

This:

   <Component prop="value" />
directly translates to this:

   React.createElement(Component, {prop: "value"}, null);
You can skip JSX entirely, and use functions only. If that's too verbose for you, some people alias `React.createElement` to `h` à la Hyperscript[1] and that becomes

   h(Component, {prop: "value"}, null)

That's why, unlike the not-really-HTML-not-really-javascript templates of Vue and Angular, JSX is not markup. It's 99% plain old Javascript, 1% paper-thin DSL.

[1] https://github.com/hyperhype/hyperscript

Re:Redux – Not Dead Yet by kabes on Wednesday April 25, 2018 @05:35AM (#16919906)
For me JSX really feels like old-school PHP, we all decided this sucks:

  <?php
  if (condition) {
  ?>
  <div>whatever</div>
  <?php
  }
  ?>
Yet in react everybody seems happy to do the exact same thing in javascript.
Re:Redux – Not Dead Yet by seattle_spring on Wednesday April 25, 2018 @06:10AM (#16920107)
It's not the exact same thing at all though. Declarative versus imperative. I'm sure someone's going to hem and haw about how they're the "effectively" the same, but they're really not. JSX is a breath of fresh air for front end development.
Re:Redux – Not Dead Yet by kabes on Wednesday April 25, 2018 @06:31AM (#16920213)
Straight from the react docs [1]:

  if (isLoggedIn) {
    return <UserGreeting />;
  }
How is that different?

[1] https://reactjs.org/docs/conditional-rendering.html

Re:Redux – Not Dead Yet by winterbe on Wednesday April 25, 2018 @09:11AM (#16921195)
The difference is that the JSX snippet is translated in plain JavaScript which means you can automatically check for syntax errors, type errors (when using TypeScript or Flow) and semantical errors (by writing simple unit tests), whereas the PHP snippet is just a big mess. JSX also runs on clients, servers and mobile devices which is a big plus to me.

This is fine:

  <?php
  if (condition) {
  ?>
  <div>whatever</div>
  <?php
  {
  ?>
this not:

  if (isLoggedIn) {
    return <UserGreeting />;
  {
Re:Redux – Not Dead Yet by dmitriid on Wednesday April 25, 2018 @09:36AM (#16921373)
Re:Redux – Not Dead Yet by tootie on Wednesday April 25, 2018 @06:29PM (#16926904)
This is the official example of using context from the react doc

  function 
   ThemedButton(props) {
     return (
       <ThemeContext.Consumer>
         {theme => (
           <button
             {...props}
             style=. 
   {{backgroundColor: 
   theme.background}}
          />
        )}
      </ThemeContext.Consumer>
    );
  }
I've been writing JavaScript for 15 years and this looks like gibberish to me. There's at least 3 context switches between JavaScript, JSX and CSS. We're abandoning separation of concerns and creating a monstrosity of programming by punctuation.
Re:Redux – Not Dead Yet by Sacho on Wednesday April 25, 2018 @08:57AM (#16921083)
Er...why does this suck? What do you replace it with?
Re:Redux – Not Dead Yet by lmm on Wednesday April 25, 2018 @10:50AM (#16921984)
It sucks because you can't even parse it, you can't understand what's going on without mentally switching back and forth between markup and programming language even if the two are in the same file. You replace it with a framework that lets you treat HTML fragments as values in your programming language and write plain old code that follows the normal rules of the language, that can be refactored following the normal rules.
Re:Redux – Not Dead Yet by dmitriid on Wednesday April 25, 2018 @02:48PM (#16924726)
Hmm... JSX isn't HTML. I thought we established that.

Moreover, JSX components are plain old Javascript functions and classes. You write plain old code that follows normal rules if the language when you deal with them.

JSX DSL is there for convenience because it's, well, extremely convenient.

There are feeble attempts elsewhere to equate JSX with writing HTML inside PHP tags which fall apart the moment you give JSX more than a give-second cursory glance.

Re:Redux – Not Dead Yet by lmm on Wednesday April 25, 2018 @06:49PM (#16927081)
I wasn't talking about JSX one way or the other - just trying to explain what the issue with PHP was and how the industry moved away from it.
Re:Redux – Not Dead Yet by dmitriid on Thursday April 26, 2018 @02:16AM (#16929247)
Yes. But... JSX is not HTML. And no, the issues PHP had with embedding HTML do not apply to JSX.

JSX is a paper-thin DSL on top of Javascript. JSX components are regular Javascript functions and classes. You work with them the same way you work with any function it class in JS. Unlike PHP where it was actual HTML interspersed with PHP code.

Re:Redux – Not Dead Yet by kabes on Thursday April 26, 2018 @04:23AM (#16929996)
I get that JSX is a DSL that ends up being javascript. But there are a lot of arguments why we don't write HTML directly between the PHP which still apply to JSX, even though it's not HTML. For me as a programmer it doesn't matter what happens under the hood.

Whether I type `return (<button>foo</button)` in react or echo '<button>foo</button>' in php, it's the same thing from my point of view as an application builder.

Re:Redux – Not Dead Yet by lmm on Thursday April 26, 2018 @08:11AM (#16931115)
> there are a lot of arguments why we don't write HTML directly between the PHP which still apply to JSX, even though it's not HTML.

Such as? This is the point in dispute, so you need to actually make specific arguments if you think they apply.

Re:Redux – Not Dead Yet by skocznymroczny on Wednesday April 25, 2018 @05:43AM (#16919961)
In the webdev world, everything old is new again