Live data from Hacker News

Simple React Patterns

lucasmreis.github.io

41–50 of 87 posts

Re: Simple React Patterns

#41
post #28

Earlier quoted context omitted.

Then the response body is parsed to JSON The "res => res.json()" call? But what does this do? res seems to be used nowhere and just discarded without any side effects.

This is a shorthand for function (r) { return r.json()} Note the extra return. We return the JSON decoded response that is our planet. and use its value to set state if no error occured

Got it! Thanks!

Re: Simple React Patterns

#42
post #28

Earlier quoted context omitted.

Then the response body is parsed to JSON The "res => res.json()" call? But what does this do? res seems to be used nowhere and just discarded without any side effects.

If it makes it easier to reason about, it's equivalent to: // The Promise resolves when the server starts responding const response = await fetch(…); // The Promise resolves when both the transfer and the JSON parsing ended const body = await response.json(); So, yes, `response` is only used once and then discarded. `fetch()` may fool you into thinking that it resolves when the request is complete, but it's always a…

Ah! The equivalent with await seems much clearer to me.

Re: Simple React Patterns

#43
post #39

After 2+ years of writing React most of the day every day with a lot of different patterns coming in and falling out of love in our app, our experience falls almost exactly in line with this. It's simpler than I would have imagined from when we first started using React/Redux and handles the most complex interfaces our app has. My favorite part is how he models the different states the component can have. He describe…

> He describes the different states in a comment block but we found it even nicer to use Flow and disjoint unions to help the developer avoid impossible states

I'm on the highway to Elm

(or reason, or purescript, either way towards a language with first-class sum types support, that's just so convenient)

Re: Simple React Patterns

#44
post #3

Earlier quoted context omitted.

React explicitly discourages using inheritance with components, instead promoting composing components together like functions. You're also encouraged to keep them stateless if possible, so they create the same output for the same input. I'll need some convincing that functional programming is on its way out in JavaScript.

Yeah, but why did they switch from `React.createClass({..})` to the class syntax? Classes are terrible in JS, they're not even native JS, it doesn't fit in JS. This change alone has brought more and more OO JS code.

Classes in JS6 are just a syntax sugar for the native JS prototype inheritance.

Nothing "terrible", absolutely native (both since the keyword is part of JS and since the implementation is based on prototypes that have been with JS since the beginning), and fits perfectly.

Re: Simple React Patterns

#45
post #28
post #26

Earlier quoted context omitted.

When the component is mounted on the page, a planet is fetched from an api. Then the response body is parsed to JSON. After that the object is added to the component state. When there was an error it is added to the state instead.

Then the response body is parsed to JSON The "res => res.json()" call? But what does this do? res seems to be used nowhere and just discarded without any side effects.

If it's a simple statement of this form, the result of the expression is returned.

so res => res.json() is just like having a callback like:

function(res) { return res.json() }

and the next part in the then() chain, takes that result as "planet".

Basically, that last part of the chain, has a signature like:

(function callback(...), function errorCallback(...))

and the callback part gets the result of the previous step as "planet" (if all went ok). If there was an error in the previous steps of the chain, the errorCallback would get called, with the exception passed in as "error".

Re: Simple React Patterns

#46

The problem with resolving promises inside of `componentDidMount` is that there is no way to cancel those promises. If the component is unmounted, and then setState is called, it will generate an error (or a warning. I can't remember). Facebook discusses this on its blog: https://reactjs.org/blog/2015/12/16/ismounted-antipattern.ht... I've been using Observables which, in my experience, bind a little cleaner than Pro…

it would be nice if React.Component had a cancel method, so you could simply drop all your promises into it and they get canceled when the component unmounts.

    this.cancelOnUnmoun(new Promise(...)).then(...)

Re: Simple React Patterns

#47
post #2

Part of me is sad that Javascript has now been made OOP, after a majority of efforts pushing the balance towards becoming more and more functional (e.g. PureScript) only a few years ago.

actually once redux is introduced to react it's very very very functional.. so hold the sadness

Re: Simple React Patterns

#48

Earlier quoted context omitted.

When you say "cancellable promises", that means that you cannot use native JS promises, correct?

You can, you just need to wrap it when used inside a react component.

How?

I've thought it's just technically impossible (no API to do so) to cancel a `fetch` request in progress.

Re: Simple React Patterns

#49
post #47
post #2

Part of me is sad that Javascript has now been made OOP, after a majority of efforts pushing the balance towards becoming more and more functional (e.g. PureScript) only a few years ago.

actually once redux is introduced to react it's very very very functional.. so hold the sadness

React is functional even without Redux :-)

Look at the Reason team, with their Reason-React implementation. Cheng Lou seems to actively dislike a single store that Redux is, and, if anything, co-locates stores to stateful components.

Re: Simple React Patterns

#50

The problem with resolving promises inside of `componentDidMount` is that there is no way to cancel those promises. If the component is unmounted, and then setState is called, it will generate an error (or a warning. I can't remember). Facebook discusses this on its blog: https://reactjs.org/blog/2015/12/16/ismounted-antipattern.ht... I've been using Observables which, in my experience, bind a little cleaner than Pro…

Yes there was a whole discussion in a github issue about the correct pattern to solve this. Creating and guarding against a `isMounted` field in your `.then()` callbacks means you prevent garbage collection of the unmounted components. An easy solution is to use cancellable promises (with some subtleties to allow garbage collection). There's an implementation which take care of this properly here (provides an HoC): h…

Promises? Cancellation? The whole point of a Promise is that it must resolve to something eventually.
Post reply on HN