I stopped learning React because of bind()

I stopped learning React because of bind()

I remember the first time I tried learning React I got to the bind()? function(having never learned about it in JavaScript or the “this” keyword for that matter) and gave up.? I couldn’t wrap my mind around it and the tutorial I was using never cleared up my doubts about it.? I had a feeling that there was something that I was missing from my study of JavaScript.? So, I decided to go back to study it some more.

When I first tried to learn JavaScript. I used freecodecamp.? I think that it’s a great free resource for beginners.? When I first started learning I was using the legacy material before it was called legacy.? After completing the first algorithms section I moved on to start building projects, but this was a mistake and I will explain why.

I think many people make this same error.? They’re in a rush to get a job so they don’t fully master(or learn a sufficient amount to feel comfortable with a framework) JavaScript before moving on to a front-end framework.? I decided to revisit the fundamentals of JavaScript using an udemy course.

In this course I learned about objects with functions and how the aforementioned bind() function is used with those functions to bind the “this” keyword(which refers to the object calling the function) to a certain object.? This is for cases where different objects want to use the same function that makes use of the “this” keyword.??

What was really interesting about the example used was the fact that the function was actually contained in one of the objects.? I will attempt to simplify and generalize the example. So you have object A which has a function called doSomething, which uses variables from the object using the “this” keyword.? Object b can also call that function but you have to remember to bind the “this” keyword to object b like the following:

A beginner understanding

const objectA = {

variableA : 1,

variableB : 2,

doSomething(){

return this.variableA + this.variableB

}

}

const objectB = {

variableA = 1,

variableB = 3,

}

const ds = objectA.doSomething;

//here the “this” keyword is now undefined because the function exists outside of an object.

const obds = ds.bind(objectB);

//now the “this” keyword refers to objectB in obds

objectA.doSomething(); // 3 expected

obds(); //4 expected

You can ofcourse add parameters to the function and send arguments when you call it.? How would that look?

Next level understanding

const objectC = {

variableA: 1,

variableB: 2,

doSomething(variableC){

return this.variableA + this.variableB + variableC;

},

};

console.log(objectC.doSomething(2));//expected 5

const dsC = objectC.doSomething;//

const obdsC = dsC.bind(objectB);

console.log(obdsC(2)); //expect 6

Another case where you would need to use the bind() function is when calling an event listener, because the “this” keyword gets assigned to the element calling the event listener rather than to the object that the function(or method I should say) is contained in.

let objectD = {

counter: 0,

?? incrementer(){

???? this.counter++;

???? console.log('counter is',this.counter);

?? },

};

let btn = document.getElementById('btn');

function updateBTN(){

??btn.innerText = objectD.counter;

}

const updateCounter = objectD.incrementer.bind(objectD);

//notice that if you delete .bind(objectD) from the above line you will get "counter is NaN" in console when you click the button

btn.addEventListener('click',() =>{

??updateCounter();

??updateBTN();

});

Bind can also be used to set other values, not just the value of this.? For example lets say you want to copy a function that has several parameters and you want to set some to default values.

Somewhat advanced level understanding

//use bind to set default values for parameters when copying functions

const employeeDiscount = function(rate, item){

?? console.log('item',item,'rate',rate)

?? return item - (item * rate);

}

//in the following the first argument is for "this" but "this" but it's not being used.? It's? function that we're copying not a method.? So we set it to null. The next argument matches the parameters of the function employeeDiscount

const managerDiscount = employeeDiscount.bind(null, 0.2);

//if you just do employeeDiscount(0.2) you'll get item undefined

console.log('managerDiscount',managerDiscount(10))//expected 8

Link to a codepen of the above code:? https://codepen.io/king-jones-frazier/pen/OJYgBYa?editors=1111

A good stackoverflow question I found that exemplifies the importance of learning Javascript well before jumping into React.? All of the answers mentioned how the poster's misunderstanding has to do with not understanding a certain aspect of JavaScript rather than the problem being with React. https://stackoverflow.com/questions/58933988/reactjs-bind-method-to-class-component

Now how does this relate to bind in React?? That I will save for another article.

要查看或添加评论,请登录

King Jones-Frazier的更多文章

  • How I used ChatGPT to upgrade my app

    How I used ChatGPT to upgrade my app

    Some time ago I built a flashcard app for learning the top 200 drugs which you need to know to become a pharmacy tech…

  • My first virtual hackathon experience

    My first virtual hackathon experience

    Questions that I answer in this post will be: What inspired me to do a hackathon? Where did I find this hackathon? What…

  • How I finally grokked React and made my first contribution to a project on Github

    How I finally grokked React and made my first contribution to a project on Github

    Sometime ago I joined a discord that you can join at the following link: https://discord.gg/M3yEGnwk.

  • Pharmacy Website Update 2.5

    Pharmacy Website Update 2.5

    I talked to some pharmacy interns and pharmacists that I work with about what they use to study drugs. I learned that…

  • Pharmacy Website Update 2

    Pharmacy Website Update 2

    What I’ve added: Flashcard view tracking with progress bar with the goal being 90 views. Added auth to the flashcard…

  • Pharmacy Website Project Update

    Pharmacy Website Project Update

    I will give an update on my pharmacy education website project, what have I added to it and what have I learned. I…

    2 条评论
  • Review of David Burkus Friend of a Friend

    Review of David Burkus Friend of a Friend

    This is a book about networking. But it’s not about how to introduce yourself at your next meetup for business or tech…

社区洞察

其他会员也浏览了