Live data from Hacker News

JavaScript fundamentals before learning React

robinwieruch.de

41–50 of 72 posts

Re: JavaScript fundamentals before learning React

#41
post #38
post #37

Earlier quoted context omitted.

I usually do this when I need to pass arguements in: this.smth(arg) }> How can I do this without defining a function?

isn't that functionally equivalent to `onClick={ this.smth }`?

No it is not equal because the former autobinds `this` into the body of the arrow function, however the latter is "just" a function pointer hence will not be called with the correct `this` value. One way to get around this is using `this.smth.bind(this)`, which binds the correct `this` for the later execution.

Re: JavaScript fundamentals before learning React

#42
post #8

Hey, author here :) I am curious about your experiences using/learning React. Are there any other JavaScript topics which are important when starting out with React? Would be great hearing your opinion!

This is great! Great write up and good idea on what to actually write about. One suggestion I'd make is to use ES6 template literals over concatenation, since you're using other ES6 constructs anyway. getName() { return `${this.firstname} ${this.lastname}`; } Cleaner and less error prone. On our team we had a convention to only use backticks when we intended to combine strings, so that if you ever saw a backtick, you…

Included it :)

Re: JavaScript fundamentals before learning React

#43
post #8

Hey, author here :) I am curious about your experiences using/learning React. Are there any other JavaScript topics which are important when starting out with React? Would be great hearing your opinion!

Formatting strings (template literals [1]) comes up frequently for constructing ids/refs/various other props. 1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Included :)

Re: JavaScript fundamentals before learning React

#44
post #38
post #37

Earlier quoted context omitted.

I usually do this when I need to pass arguements in: this.smth(arg) }> How can I do this without defining a function?

isn't that functionally equivalent to `onClick={ this.smth }`?

no, because you're not passing the arg in now right?

Re: JavaScript fundamentals before learning React

#45
post #13
post #8

Hey, author here :) I am curious about your experiences using/learning React. Are there any other JavaScript topics which are important when starting out with React? Would be great hearing your opinion!

hey there, nice write-up two minor things that you might include are 1: rest in the context of function args 2: short circuiting: e.g. `isLoading && `

Included. Thanks :)

Re: JavaScript fundamentals before learning React

#46
post #8

Hey, author here :) I am curious about your experiences using/learning React. Are there any other JavaScript topics which are important when starting out with React? Would be great hearing your opinion!

I am learning React and ES6 currently. I dove into React before really understanding ES6, and your article is really helping to illuminate some of the dark corners of JS. Thank you. One section I was hoping would get more treatment in your article (like the same treatment you gave classes, which was great) was imports/exports. Named vs. default exports are kind of baffling for a newcomer, and the usage of the named i…

Extended the section about imports and exports. Thanks for your feedback!

Re: JavaScript fundamentals before learning React

#47
post #37
post #31

Earlier quoted context omitted.

I think, the most important aspect I tell my developers is to not define functions inline during render. So, do not do this: this.smth() }> This creates a new function every time the rendering happens and mutates the prop onClick on every render. Once you have a component with a lot of elements (e.g. inputs) and child components that check for changes props to determine whether to render, this will get you in perform…

I usually do this when I need to pass arguements in: this.smth(arg) }> How can I do this without defining a function?

> How can I do this without defining a function?

You do define a function, but only once. Constructor:

  this.smth = this.smth.bind(this);
JSX:

  
Kind of awkward, but the standard practice last I checked. Arrow methods (terminology?) are also something you can add to the language that does the equivalent of .bind() replacements.

Re: JavaScript fundamentals before learning React

#48
post #37
post #31

Earlier quoted context omitted.

I think, the most important aspect I tell my developers is to not define functions inline during render. So, do not do this: this.smth() }> This creates a new function every time the rendering happens and mutates the prop onClick on every render. Once you have a component with a lot of elements (e.g. inputs) and child components that check for changes props to determine whether to render, this will get you in perform…

I usually do this when I need to pass arguements in: this.smth(arg) }> How can I do this without defining a function?

For simple use cases, we also use this pattern, however, usually moved to separate functions for readability, real example:

    createOpener(folder) {
        return () => this.props.dispatch(Actions.listFiles(folder, this.props.member));
    }
As said, for more complex layouts we need to reduce moving parts. For example, we have input masks that can easily consist of 200 input fields alone plus all kinds of other components. What we do in that case is usually pre-binding functions with arguments. Roughly like this:

    // Target function
    onItemClicked(item) {
        // ...
    }
    
    preBind() {
        const { data } = this.props;

        // Bind with primary key
        data.forEach(item => {
            this[`__boundFn_data_${item.get('primaryKey')}`] = this.onItemClicked.bind(this, item);
        })
    }

    render() {
        const { data } = this.props;

        return 
            {
                data.map(item => {
                    const pk = item.get('primaryKey');

                    return { item.get('label') }
                })
            }
        
    }
This approach involves a lot more complexity concerning removing bound functions and caching. And things like function name generation is stored in separate functions etc. It's not trivial but you get some performance out of it.

By doing this, though, we can rely on props checks for components to determined the necessity of rendering which allows us to use React's PureComponent in 90% of our components.

Re: JavaScript fundamentals before learning React

#49
post #14
post #8

Hey, author here :) I am curious about your experiences using/learning React. Are there any other JavaScript topics which are important when starting out with React? Would be great hearing your opinion!

Nice writeup! I wrote a tutorial that would be the next step to yours, called React From Zero[0] I try to teach React here with the basic JavaScript knowledge most people already have. [0] https://github.com/kay-is/react-from-zero

I can recommend this, having only read the first two, very enlightening. Thank you for your work.

Re: JavaScript fundamentals before learning React

#50

I had the chance/experience to train a new team in Vue.js (and thus modern JS) which I imagine is similar to people learning React. I noticed the same thing as the author - the new JS frameworks are "all about JavaScript". Without a solid knowledge of how JS works, and ES6, learning any of them is difficult. When realizing this, the main things I focused on what getting people to understand JavaScript, _then_ start t…

This was my personal experience learning Vue, I didn't know as much about JS as I thought I did.

I went back and started again filling in all the gaps I obviously had.

A year later and the frontend for all my stuff at work is TypeScript classes over Vue components and the code is actually simpler to understand.

I learnt something and the code got better, a win/win like that is rare ime.

Post reply on HN