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.
Simple React Patterns
21–30 of 87 posts
Re: Simple React Patterns
#22Earlier quoted context omitted.
In React v0.13 https://reactjs.org/blog/2015/03/10/react-v0.13.html , they claimed the reason for the switch is because the `class` syntax is for "more flexibility." What can you do with the `class` syntax that you can't do with the functional syntax? Furthermore, you can generate React code much easier with the functional syntax than with the class syntax. Also, the functional syntax has mixins and other ways to com…
Seems like a weird thing to miss for someone who ostensibly prefers functional programming. For example, people here will talk about `function User() {}` like it's the pinnacle of amazing abstraction. Because it has the word "function" in it or something. There's nothing functional about that to me. Mutating the prototype and dealing with the implicit `this` variable in your functions is about as far away from functi…
Which stable and widely used GUI kit has ever been done well with functional programming? Until one exists, I can’t really take anybody who pushes functional programming for GUIs very seriously.
Functional programming just isn’t that good for complex domains. OOP is way better for GUIs.
Re: Simple React Patterns
#23 componentDidMount() {
fetch("https://swapi.co/api/planets/5")
.then(res => res.json())
.then(
planet => this.setState({ loading: false, planet }),
error => this.setState({ loading: false, error })
);
}Re: Simple React Patterns
#24Earlier 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.
Re: Simple React Patterns
#25This part seems strange. What is it supposed to do? componentDidMount() { fetch("https://swapi.co/api/planets/5") .then(res => res.json()) .then( planet => this.setState({ loading: false, planet }), error => this.setState({ loading: false, error }) ); }
Re: Simple React Patterns
#26This part seems strange. What is it supposed to do? componentDidMount() { fetch("https://swapi.co/api/planets/5") .then(res => res.json()) .then( planet => this.setState({ loading: false, planet }), error => this.setState({ loading: false, error }) ); }
Re: Simple React Patterns
#27The 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…
Re: Simple React Patterns
#28This part seems strange. What is it supposed to do? componentDidMount() { fetch("https://swapi.co/api/planets/5") .then(res => res.json()) .then( planet => this.setState({ loading: false, planet }), error => this.setState({ loading: false, error }) ); }
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.Re: Simple React Patterns
#29This part seems strange. What is it supposed to do? componentDidMount() { fetch("https://swapi.co/api/planets/5") .then(res => res.json()) .then( planet => this.setState({ loading: false, planet }), error => this.setState({ loading: false, error }) ); }
It fetches data from an endpoint then sets component state based on the response
Re: Simple React Patterns
#30Earlier 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.
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