React tutorial in Hindi #12 State with class component
HTML-код
- Опубликовано: 9 сен 2024
- in this react js tutorial for beginners series we learn how to use state in a class component in react js. This video is made by anil Sidhu in the Hindi Language.
Playlist for complete React series in Hindi
link • React tutorial for beg...
git github.com/ani...
State in Class Component
Make Class Component
What is state
Define state
Update state with a Button click
Interview Question
Join on Insta @php.step.by.step
Thanks a lot sir, I have learned many things.
No, we can't use useState in class-component because, useState is a Hook and Hooks are not implement in class-component.
Right
In React, state is not public, state is private to the component in which it is defined. This means that the state is not accessible or modifiable by other components directly.
React Class Components have built in State Object. You might have noticed that we used state earlier in the component constructor section. The state object is where you store property values that belongs to the component. When the state object changes, the component re-render.
ONLY call useState() hooks inside a functional component: Hooks are created for functional components and should only be used in functional components. They don't work in class components. If you need to use state in the class component, then use the state object.
clearly explained!
ur videos are ultimate keep up the good work thanks
op lecture sir
Sir I have a small doubt regarding state, apne bola state public hai but google mai private show kr raha hai. So which one is correct, please clear it.
Anyone also can verify me about this doubt.
In React, state can be either public or private.
Public State: Public state refers to the state that is accessible and modifiable from outside the component. It's typically used when the state needs to be shared across multiple components or when you want to update the state from a parent component. Public state is managed using the props system, where a parent component passes down state as props to its child components. This makes the state public and accessible to those child components.
Private State: Private state refers to the state that is local and specific to a component. It's not directly accessible or modifiable from outside the component. Private state is managed using the useState or other state management tools within the component. It's used when you want to keep the state isolated to a specific component and avoid unintended interactions with other components.
let me know if you still don't understand, I'll give you code example for better understanding
Is state public or private? you said, its public then we should be able to use it outside of component. And also you said, state can not be used outside the component. These two statements conflicts each other.
I think state in react is private, not public. what do you think? Please reply here.
bhai google me state private dikha rahi he
this.setState is onestep behind. tried with await in SetState but that also not working. what might be wrong.When I am testing api with postman .Getting proper result but at the time of SetState it is one step behind when I console warn after assigning state
Use Arrow Function In OnClick Then Use this.setState
No,we can't use hooks inside the class component
Can we only define the state without giving any value or data?
Yes empty
Maza aagya sir 🔥🔥
Thankyou Bro
welcome
Hello sir i have been watching your this series but it seems to me difficult as I am a beginner.plz make a videos of react which will have small code snippets as an examples to understand it better..
Hi Sachin, you may try to look on this ruclips.net/channel/UCjL4SPbBlJoOKT_YZz6zISQ
u should have basic knowledge of html n css that's it and javascript too
if i am using 2 this.state in the constructor() like this
constructor()
{
super();
this.state={data:"Himanshu"}
this.state={number:1}
}
but i am not getting any output on the browser until i click the button
return (
{this.state.data}
{this.state.number}
this.Inc()}>Surname
);
Can you tell me why ?
@himanshuchauhan6520 bro you don't need to create a separate state just put all the state data together like this
super();
this.state={data:"Himanshusu", number:1}
this will work
@@ChandanSingh-ww5hr yes because mentioning this.state two times is wrong because now they are now different objects.
Sir apne bola he ke state public hote hy lekin chatgpt to private btaa rhee he ap ye confirm kar den...
Bhai very gud yaar, thanks bade bhai
Bss insta pe msg ka reply kar diya karo, and video me 5-7 min leke css ko bhi re-render karke dikha diya karo bhai
Sir why we are not use function keyword in that case
Sir state private hoti h na usko as a argument use krte h hm log dusre components me
No state public hoti h previous video me bataya h
Redux ke video kab aayege?
Why we can't use this.apple here?
How to update state immediately using class component?
Because whenever I try to do setState, it doesn't update immediately.
We can use useEffect in functional components
But what about class components?
How can we do it?
use componentDidMount life cycle
@@aashitkumarsinha3459 How to make it work for last update of setState?
@@sanketdeogade it get automatically render at time of states get loaded first time, but it also get called by React...when the state gets changes...
I think no we cannot use useState in class component because it gives error
Right
because it is hook. Hooks are for function components only.
hook in only for function component
states are public ?is it right?i think private is right
He mentioned in his previous video that state is public
yes bro I also think state is private
we can use it
example.
const [state, setState] = useState({
count: 0
});
console.log(state.count);
and update it like this
setState({ count: state.count + 1 });
itna bareek kar rakha text dikh bhi ni raha
how state is public ?
State is private
STATE IS PRIVATE, NOT PUBLIC
state is private not public
laptop me charge bhi diya karo sir 🤣🤣
Haha
ONLY call useState() hooks inside a functional component: Hooks are created for functional components and should only be used in functional components. They don't work in class components
No Nahe