Hi and welcome to this exciting tutorial
about React Native class-based components with state and event management.
I want to talk to you about state a bit.
State is a crucial concept in React Native
and it refers to the data or variables that determine a component's behavior
and render information to the user.
In class-based components state is created
using the constructor we would create our
state here like this, this state and then empty object.
That's it.
So this is the initial state of our
components and now let's see how we can update the state in response
to the simplest event such as clicking on a text component.
So let's create an onPress event here and in order to update the state all we
need to do is say for example this set state and pass an object saying
name Nata for example let's save this and I have an error because I forgot one brace.
Great.
And now let's see what would happen if I put this state name here.
I'm just going to put a question mark because it is initially not defined so we
don't want to get an error and before we click on this we see hello and that's it.
But after we click on this we're going to see hello Nata.
Because our state was reset,
it's important to note that state should only be updated using set state and never
directly as this can lead to unexpected behavior in your components and any change
will rerender the component and it will ensure that component updates
with the latest state and displays the updated information to the user.
So that's a brief introduction to state
in React Native and how it works in class based components.
And in the next video we'll talk about what lifecycle methods are and how they
can be used to control and manage the behavior of your component.
Thanks for watching and see you in the next video.