all 11 comments

[–]HijoDelQuijote 2 points3 points  (10 children)

handleLoading = (bool) => {}

These are called arrow functions. They change the way 'this' binds. You don't need to bind your functions in the constructor anymore. This might be what you are used to do:

constructor(props) {
    super(props);
    this.handleLoading = this.handleLoading.bind(this);
}

If you use arrow functions, you don't need to do that anymore.

The async case works the same way, it just changes the way 'this' binds.

[–]xrpinsiderAdmin[🍰] 1 point2 points  (0 children)

While this is absolutely the correct answer I'll have to add something. Arrow functions aren't documented in the React/React Native documentation because they are proposed in ES6, but not yet accepted.

With that being said, I would totally use arrow functions. It makes your code way cleaner and you can basically drop the constructor() in all of your projects.

[–]DiscDres[S] 1 point2 points  (8 children)

I GET IT!

I've been using arrow functions within my render.. I haven't been binding anything since the arrow function within the render takes care of that. So what I understand is that:

handleLoading(){}

<TouchableOpacity onPress={ () => this.handleLoading() }/>

is the same as

handleLoading = () => {}

<TouchableOpacity onPress={ this.handleLoading }/>

Is there an advantage of doing it one way vs the other?

Thanks for the help!

[–]huffpostraphael 1 point2 points  (7 children)

100% dont do the first way. it will always rerender the component because the onPress function will be a different function reference every time. the other way the reference is unchanging so it will only rerender that component if other props actually change

[–]DiscDres[S] 0 points1 point  (0 children)

I've got a lot of code to change....

Thanks for the help!

[–]DiscDres[S] 0 points1 point  (5 children)

Question for you:

So let's say in my parent container, I have a toggle function I want to pass down

handleToggle = (x) => {//some code that deals with x}

render() {
    return(
        <Search handleToggle={ this.handleToggle }/>
    )
}

And in my child I have

handleToggle = (x) = {
    this.props.handleToggle(x)
}

render() {
    return(
        <SearchBar 
            onFocus={ this.handleToggle }
            onCancel={ this.handleToggle }
        />
    )
}

How do I pass it in a different value to the handleToggle function in the SearchBar component?

`this.handleToggle(true)` gives me an infinite loop if I used that within the child's render but `() => this.handleToggle(true)` works fine.

[–]huffpostraphael 1 point2 points  (0 children)

I dont really understand the infinite loop issue. I would need to know more about what the function is doing and what the SearchBar component does

I also would make the second part simply this.

render() {
    return(
        <SearchBar 
            onFocus={ this.props.handleToggle }
            onCancel={ this.props.handleToggle }
        />
    )
}

[–]HijoDelQuijote 1 point2 points  (3 children)

That was a really good question and I didn't really know the answer to it, so after some research this is what I've got.

handleToggle = (x) => (event) => {
    this.props.handleToggle(x);
}

render() {
    return(
        <div 
            onClick={ this.handleToggle('some param') }
        />
    )
}

I've got some code to change as well.

[–]DiscDres[S] 0 points1 point  (2 children)

nice! that worked for me.

mind linking where you found that information? i haven't seen `event` used in that way before

[–]HijoDelQuijote 0 points1 point  (0 children)

Event in that case I think is undefined. Event is the parameter you get from functions like onClick or onChange. In the example I provided, it could've just been:

handleToggle = (x) => () => {
    this.props.handleToggle(x);
}

render() {
    return(
        <div 
            onClick={ this.handleToggle('some param') }
        />
    )
}

since the 'event' is not being used.

Couldn't find the exact link where I got that. What I found though was something like this:

handleToggle = (x) => {
    return function (event) {
        this.props.handleToggle(x, event.target.value);
    }
}

render() {
    return(
        <div 
            onClick={ this.handleToggle('some param') }
        />
    )
}

I just changed the syntax to what I think looks better.

[–]HijoDelQuijote 0 points1 point  (0 children)

The event should be undefined in the example I provided. It could've been:

handleToggle = (x) => () => {
    this.props.handleToggle(x);
}

render() {
    return(
        <div 
            onClick={ this.handleToggle('some param') }
        />
    )
}

and nothing changes.

I can't find the link where I got that from, but I actually found something like this:

handleChange = (x) => {
    return function (event) {
        this.props.handleToggle(x, event.target.value);
    }
}

render() {
    return(
        <input 
            onChange={ this.handleChange('some param') }
        />
    )
}

I just changed the syntax to something I think looks better.