For those of you wondering why he used parentheses in the previous video and not here, it was because he was binding the 'this' keyword with the event handler so that he could change the state. If you're still confused just proceed to next video you will get your answer.
Man...thanks for these videos. I bought something in Udemy, and now I'm watching your videos after each Udemy chapter to get more clarity on the topic.
Very nice! You explain concepts by taking both functional and class component in account. This is indeed of much help and provides clarity! Most of your viewers wil agree to this.
So informative tutorial, you can easily follow along and understand the core concepts of react like state and props. These are really important to undersand. This guy is legend. thanx.
Brother you are next level! I am really really thankful to you for your this help. May God bless you with the best. I am taking your lectures but the problem is that i am not getting relevant examples to practice the stuff you are teaching. It will be so kind of you if you will give me some link or stuff like that so that i could practice the concepts. Thank you so much!!! :)
@Codevolution, Thanks ! One question, why I don't see multiple logs when I click the button multiple times? I just see a counter next to the file name getting incremented on clicks.
So I have a habit of pausing the video and trying it out on my own. As soon I saw him doing event handling, I paused the video and started writing the code myself. I mean, easy-peasey 😜 , what could go wrong with JS ? And I added this : onClick = {clickHandler()} - I reloaded and saw the console log already there without the button being clicked - got my answer as soon as I resumed at @3:17 🤦♂ Long Live JavaScript!
One thing I will add here even me at first glance got confused that ,Sir used function keyword ( function eventHandler () ) in function base components but didnot use that when using class i.e , eventHandle(){ console.log("button clicked") the reason is it is same syntax as or pure Js for classes like we add method in Js class like getResult(){ console.log("hello")} so functions are method in classe,no need of using function keyword hope this help
Hi, I have a doubt. Please see the below code. import React, {Component} from 'react'; class Counter extends Component{ constructor(props) { super(props) this.state = { counter: 0 } } incrementCounter(){ this.setState({ counter: this.state.counter + 1 }, () => console.log("count increased to ", this.state.counter)) } render() { return Count - {this.state.counter} this.incrementCounter()}>Increment } } export default Counter; When I run the above program, count increases by 1 when I click the button. But according to the tutorial when I remove parantheses to the incrementCounter the counter doesn't increase. Can anyone explain when?
Hi I'm following your videos step by step and i have a question, why didn't you use arrow function in the handler same as you did in video 10 &11 ? and can we? thank you so much
I have a question. here you said we are not using round brackets as the it will be the function call. so, why in previous videos of counter we used this.changeMessage()}> There we used round brackets ? why?? please Explain??
Thanks a lot for your awesome course. I wonder why in the functional component you used the word "function" (function clickHandler()), while in the class one not (clickHandler()). Cheers!
Hey thanks for the video. Amazing . I just had one question. In setState video while calling increment(), why did we have to use arrow function and here in class we just used this.functionName. I tried increment thing this way but it gave an error. Kindly explain! Thanks
Is it wrong to do it this way, it's working: function kliknuto() { alert('Button is CLICKED!'); } function overovano() { alert('Button is HOVERED!'); } function EvHandler() { return ( kliknuto()} onMouseOver={() => overovano()}>Dugme ) } ???
Can anyone answer this question for me? (thanks in advance) ..... in the Class Component, at 4:40 , why do we NOT need the constructor and call to super() ??? i thought we needed those for call components? apparently i missed something. :/
I have a query. Please resolve it. When we assign the onClick event handler with a function call, then why does it not log to the console on being clicked? What are the internals of having an event handler as a function call?
Hope this helps : see when you add parentheses with a function name like this ->fun() JavaScript treat this like a function call and automatically calls the function so when the component renders the function is already called . so now whether you click the button 1 time or multiple times it won't work because react will not re-render the component that is because react consider the function call on the time when it was render for the first time that's why .
@@Himanshu_Sharma.. Thanks for the elaborate explanation! I have one more question. Please answer if you can . suppose I have an array defined in this.state={arr:[]} and onClick of a button I want to push no. of times button clicked to arr then how can we do it? Basically I am confused with updating state of an array when an event is fired.
Because functions in class are methods and not regular function as well as they are the part of the class. Also I am adding this : you cannot define a function inside render in a normal way you can only use arrow functions
cause it is pure Javascript Syntax if you had read about how to use class in Js and how functions (means method) are added in That class so the same rule here too like there means in pure js we can use like getResult(){ console.log("hello")} so this is how function(we can it method in class) are used in class
I really like the series so far. I am having a bit of confusion earlier in onClick(eg. Counter.js) we were passing an arrow function to call the handler but here we are just calling it by name. How do we differentiate when to use what.?
I am not an expert, but I will try to answer your question. if you define a function, which does not take any arguments, then you can give just the handler the function name, like onClick={functionName}. but if the function needs arguments, then you would have to use parenthesis to pass it like onClick={functionName('hello')}. once you do this, it is calling the function. so it returns void. but the onClick handler needs a function, not void. so you would have to use arrow function syntax if you need to pass an agurement. try it in vscode yourself, define function functionName() { console.log('hello') } and you can then use onClick={functionName} . Now, define function functionName(name){ console.log(name)}, and vscode will error saying the function call is returning void and thats the wrong type for the onClick handler. but if you use onClick={()=>functionName('Steve')} then your giving the onClick handler what it wants, a function. an anonymous one, but a function nonetheless.
Even if i write onClick={clickHandler} rather than onClick={this.clickHandler} in js file for it is still giving the same output. I want to know is there any difference or something..? Pls pickup.
is not correct to write something like the following? class MyComponent extends Component{ function myFunction(){ } } Have to remove function, only keep myFunction()?
Is it necessary to create button in the same file in which i have my function. What if my button is in different file along with other component and i want to link the onClick fucntion which is in different file? How do i do that. I'm new to react, pls help me out.
Thanks you sir, but I have a question in setState() topic for updated value by using onClick=()👈-(that method name with paranthasis) . but in this video your using without parenthesis . Here also doing onCilck. How it is sir ...?
Yeah so what actually happens is when you put parentheses on a function it gets called on that place automatically so what happend on that video was when he clicked on the button the arrow function started and since the increment function had () the increment function got called automatically .so basically it's just another way of calling a function indirectly , I hope you understood . By the way great catch !! I was looking for this comment
yes you can---> { function clickHandler() { console.log("Button clicked") } clickHandler( ) --->here u have to call the function else this function won't be called } }>Click This is not suggested bcoz this makes the code difficult to read.
in functioncClick : function clickHandler() { console.log("button clicked"); } In classClick : handleClick() { console.log("inside class click"); } What is the difference between these two ? can anyone explain it ?
In a normal HTML application, we pass the event handler along with the round brackets inside double-quotes. What is the difference between that and passing the function reference inside curly braces in react?
wow I spent like 3 hours pulling my hair out because I could not get onclick to work, turns out I had it in lower case instead of camel case, this is such a small thing that all the other youtubers and articles miss
you can use--> { function clickHandler() { console.log("Button clicked") } clickHandler()--->here u have to call the function else this function won't be called }}>Click not suggested bcoz code is difficult to read so better define the function outside
Here is the explanation of the answer in the reply.... Yeah so what actually happens is when you put parentheses on a function it gets called on that place automatically so what happend on that video was when he clicked on the button the arrow function started and since the increment function had () the increment function got called automatically .so basically it's just another way of calling a function indirectly , I hope you understood .
this anemia :(:(:( Vishwas I wanted to use arrow function instead of regular function on clickHandler. For Functional component const FunctionClick = () => { const clickHandler = () => { console.log('Clicked'); }; return ( Click ); }; For Class component class Classclick extends Component { const clickhandler = () => { console.log('Clicked'); }; render() { return ( Pls Clicke Me ); } } This throws me an error. I had to change const clickhandler to just clickhandler. What is going on???
I'm very new at this, I'm working on learning React Native, and I can't seem to wrap my head around how the hell i should work with events outside my file. I'm currently working on an exercise that asks me to create all my components separately. I have created a file (Input.js) where i have a password input that has a function where I can access the input text the user is texting, but i can't access the event, and the event changes on my Button.js file, anyone able to help???? I'm so stuck on this one, i'd like to avoid using redux or external libraries because i've been told it's possible in this scenario! PLEASE SAVE ME FROM THIS MISERYY!!
Can create a function inside another function in functional/class component : onclick="myFunction()" vs onClick={myFunction} if added parantesis at end - {myFunction()} - it becomes function call - and method gets called, even before calling..... WoW...... also if its arrow function, ( ) is allowed, next video if its class: onClick={this.myFunction}