State Management in React Native
State Management in React Native
Red
‘Red’ ‘Green’ ‘Blue’ → Data that is changing
More Red
Number → Type of the States
Less Red
Less Green
/* 3 Different Pieces of States
We also need how different pieces of states coordinate */
Blue
More Blue
Less Blue
Buttons with “Red”, “Green” and “Blue” are almost the same except the labels. So, we can use Resuable Component.
And also, we will see how states and props work with Resuable Components.
1
Create a button in HomeScreen.js and attach it to SquareScreen.
Test. Ok.
“rnfes”:
2
Let’s pretend that we only have the color Red button and use it without prop.
3
Now, use props and send Text values to the component. With prop named “color”, we pass the labels for the
buttons.
Normally we catch the props as shown below. “color” prop is in the “props” object.
But we will use destructuring and extract “color” from “props” object and use it in the component.
4
Test. Ok.
Last thing we have to do is add in (create) state variables and modify them from the Button elements.
SquareScreen
In SquareScreen we need 3 State Vars for rgb(). And this is going to be used as backgroundColor property.
In our case, SquareScreen is the Most Parent Component. Then we create the State Vars in SquareScreen
component.
5
QUESTION: Since we are creating the State Vars in SquareScreen, how will the ColorCounter object update
the these State Vars?
SquareScreen
{value:Red} {value:Blue}
{value:Green}
Rule: If a child needs to read a state value, the parent pass it down as a prop.
In our case, child component doesn’t need to read the State Vars in parent Component. Reading or Printing
the current values are not needed.
What about if a child component ColorCounter needs to change the parents (SquareScreen) State Var?
SquareScreen
ColorCounter function can invoke that function and make change to that state var.
That will change the value of the state var → that causes Rerendering
In the next lesson, we will see how to pass these callback functions to child components to change the state vars in
parent component.
6
04. Passing Callbacks to Children
We established two important facts:
We are going to create different state variables in SquareScreen. And we will send callback functions to
ColorCounter to update these state vars in parent component
Red, green, blue state vars are defined and initialized to zero.
7
Now, we have to give to the components the ability to change these state vars. Send CallBack functions.
We send 2 Callback function to each Component. One for Increase and one for Decrease.
Actually we might think that onIncrease and onDecrease has {() => setRed(red + 1)} and {() => setRed(red - 1)}
statements in it and passed down.
8
When setRed is called, SquareScreen is rerendered. Let’s put a console.log to
see the value of state var.
We will see the values of red increases and decreases. But, we would not want it below zero or more then 255.
Test. Ok.
Callback function running. Whenever a state var is updated then the component is rerendered. Parent and Children
are also rerendered.
Next lesson, we erase console.log and do the same things for Green and Blue.
9
05. Tying State Values Together
Now apply the same callback functions to other ones:
Test. Ok. At the beginning, Red Green Blue values will be 0, 0, 0 and the color Black. Let’s try to increase them . It is
going to be very slow.
10
Let’s use 15 and define it as a constant.
Apply it:
Values should be between 0-255. Next lesson, We have to put a control for that.
11
06. Validating State Changes
Values should be between 0-255. We have to put a control for that.
• Repeated Code
• Hard to read the JSX code
12
Test. Ok. Working…. Not going over 255 and down below zero.
Next lesson add Green and Blue and refactor Helper Function.
13
Hard to read it. Refactor it with ternary expression:
14
Use setColor function for other ColorCounter Components:
15
08. Handling Text Input
If the user enters ‘s’, ‘s’ is going to be displayed: a → a, ‘al’ → al, ‘alp’ → alp …..
16
Create a button in HomeScreen:
17
09. Showing a Text Input
Import TextInput Component:
18
10. Two Important Props
Capitalization: In IOS, input by default gets capitalized. Depending on the version in Android it could be capitalized
also, or not.
Auto Correction: Generally we would want it. But, in email, username, passwords, we don’t want it.
Test. Ok.
TextScreen
TextInput
{currentText: “asdasd”}
We have a TextScreen displaying TextInput in it. Let’s accept that TextInput has a state property tied to it. Name
could be currentText. When we enter a value like “asdf”, the value of this state becomes this. In reality, TextInput
does not have a state var.
19
What if we want TextScreen to inspect the value of TextInput?
TextScreen
{currentText: “asdasd”}
I want to check the value of my child and show it on my screen. In react we can not do it. Parent can not access to
the child and read a value in it.
We will have a State Var for the child component. This State Var represents the content current in this TextInput.
TextScreen
{currentText: “asdasd”}
{value: currentText
onChangeText: () => {} }
TextInput
20
Everytime we render this TextScreen, we will show this TextInput and send these two Props.
1. Step
value: Value of the State Var in TextScreen. TextInput will get this value automatically and display it.
2. Step
Any time the user types in, this callback function is invoked. In this CallBack function, we have a state var
and we can call the setter func. Any time the user changes the data in the TextInput, we want our State var
to be updated also.
And also we pass a new value to TextInput, which is the updated text that the user just entered.
Rightnow, we don’t have any problem running TextInput. The problem is that how we pass this value to
TextScreen?
In React we don’t have a mechanism to access child from Parent. Instead, child accesses parent with this
callback mechanism. We send this callback function to child as a prop. Whenever somethings happen in the
Child, Child calls this callback function and tells the parent that something has just occured.
21
12. Updating State
Add a state var to TextScreen Component.
Send this State Var ‘name’ to TextInput as a prop and set it’s value directly. As parent we want to tell the
child what the current value it will have.
22
Whenever we change to Input value, TextScreen is rerendered with an empty string. The Input string is going
to be an empty string. To change this, we should let the TextInput component to call setName function.
setName will update the name state var and rerendereing will happen with this value.
Whenever we pass this onChangeText as prop, CallBack Function is called with a parameter named as
newValue. This value becomes the currrent value in Input area. With this newValue using setName we
update the name state var. Then TextScreen is rerendered with this new value.
23
Change code enter “Enter Name:”
13. Exercise
Enter Password
If the user enters more than 5 chars, then let that err message go away.
25