[React Native Tutorial] Lecture 5: Input and State

60
Kobkrit Viriyayudhakorn, Ph.D. CEO of iApp Technology Limited. [email protected] http://www.kobkrit.com

Transcript of [React Native Tutorial] Lecture 5: Input and State

Page 1: [React Native Tutorial] Lecture 5: Input and State

Kobkrit Viriyayudhakorn, Ph.D. CEO of iApp Technology Limited.

[email protected] http://www.kobkrit.com

Page 2: [React Native Tutorial] Lecture 5: Input and State

All source code• https://github.com/kobkrit/react-native-class-2017

Page 3: [React Native Tutorial] Lecture 5: Input and State

BMI Calculator App

Page 4: [React Native Tutorial] Lecture 5: Input and State

Making BMI Calculator App1. Draw UI on Paper

2. Initial project using react-native

1. $|> react-native init bmi

3. Making UI

4. Making Styles

5. Adding User Input Handling

6. Compute and OutputDraft on Paper

Page 5: [React Native Tutorial] Lecture 5: Input and State

Making UI1. Create container that fill

whole screen

2. Set the flexDirection of container to ‘column’ and its justifyContent: ‘center’

3. Start Adding Elements

1. Text

2. TextInput

3. TouchableOpacityDraft on Paper

Page 6: [React Native Tutorial] Lecture 5: Input and State
Page 7: [React Native Tutorial] Lecture 5: Input and State

Using view to group each items.

Page 8: [React Native Tutorial] Lecture 5: Input and State
Page 9: [React Native Tutorial] Lecture 5: Input and State

Keyboard Types (Cross-Platform)<TextInput keyboardType=‘default’>

default

numeric email-address

phone-padiOS (9.3)

Page 10: [React Native Tutorial] Lecture 5: Input and State

Keyboard Types (Cross-Platform)<TextInput keyboardType=‘default’>

default

numeric email-address

phone-padAndroid (6.0)

Page 11: [React Native Tutorial] Lecture 5: Input and State

More Keyboard Types (iOS only)<TextInput keyboardType=‘default’>

ascii-capable

numbers-punctuation number-pad

url

Page 12: [React Native Tutorial] Lecture 5: Input and State

More Keyboard Types (iOS only)<TextInput keyboardType=‘default’>

name-phone-pad

twitter web-search

decimal-pad

Page 13: [React Native Tutorial] Lecture 5: Input and State

More TextInput Attribute<TextInput keyboardType=‘email-address’ autoCaptialize=‘none’ autoCorrect={false} maxLength={30} multiline={false} placeholder=‘Please insert e-mail’ returnKeyType=‘next’/>

https://facebook.github.io/react-native/docs/textinput.html

Page 14: [React Native Tutorial] Lecture 5: Input and State

Cmd-K to Open/Hide Keyboard

Page 15: [React Native Tutorial] Lecture 5: Input and State

Password TextInput<TextInput keyboardType=‘default’ secureTextEntry={true} … />

Page 16: [React Native Tutorial] Lecture 5: Input and State

iOS -> Cmd-K to Open/Hide Keyboard

Page 17: [React Native Tutorial] Lecture 5: Input and State

Add User Input Feedback

Page 18: [React Native Tutorial] Lecture 5: Input and State

Enable JS Debugger

Cmd - D (iOS)

Menu Button (Android)

Page 19: [React Native Tutorial] Lecture 5: Input and State

State• State is a JavaScript Object that we use to track

and response to users’ inputs.

• Each React component has its own instance of state.

• Most important thing about state:

• Any change in state will cause all components or any children components inside of it to be re-rendered.

Page 20: [React Native Tutorial] Lecture 5: Input and State

State’s Rule of Thumb• When using state in component, always set the

initialize state to the reasonable values.

• Always use this.setState(changedObjects)instead of this.state.changedObject = someValue

• If not using this.setState(), React something will not acknowledge the changes. Components will not be re-rendered.

• Some strange behavior will happen in the app.

Page 21: [React Native Tutorial] Lecture 5: Input and State

Initialize State

• We first defined 3 variables in state at the class’s constructor

• weight (String) = ‘0’ - It is a string because we use it with TextInput.

• height (String) = ‘0’ - It is a string because we use it with TextInput.

• and calculated bmi (Number) = 0

Page 22: [React Native Tutorial] Lecture 5: Input and State

Showing/Setting State in TextInput

• value attribute - setting the text according to the state.weight’s value

• onChangeText attribute - invoked when user making change the TextInput, apply the new input value into the state.

Page 23: [React Native Tutorial] Lecture 5: Input and State

See State in Action

• To see state working in action, Print the state value in render()

• Run the application.

• Typing some number into Weight (KG) and then Typing some number into Height (CM) and see the difference.

Page 24: [React Native Tutorial] Lecture 5: Input and State

At start, Weight (KG) = 0, according to the weight

state’s value.

Console in React Native Debugger will print the state’s content, only when

the text in Weight(KG) is changed, because the state make the component re-render.

Page 25: [React Native Tutorial] Lecture 5: Input and State

Continue on Height TextInput

Page 26: [React Native Tutorial] Lecture 5: Input and State

Let’s compute the BMI

Page 27: [React Native Tutorial] Lecture 5: Input and State

Method Binding• Refresh the app now, input some

number, and push compute button. You will see the red screen.

• It is because compute() method is not the standard method in the component class (We added it by our own).

• The scope of compute() method is not yet visible to the class’s state.

Page 28: [React Native Tutorial] Lecture 5: Input and State

Method Binding• You can bind the scope, by using bind()

statement.

• The best place to bind the method is at the class constructor.

Page 29: [React Native Tutorial] Lecture 5: Input and State

Showing the output

Page 30: [React Native Tutorial] Lecture 5: Input and State

Full Source Code# 1

Page 31: [React Native Tutorial] Lecture 5: Input and State

Full Source Code #2

Page 32: [React Native Tutorial] Lecture 5: Input and State

Full Source Code #3

Page 33: [React Native Tutorial] Lecture 5: Input and State
Page 34: [React Native Tutorial] Lecture 5: Input and State

Exercise 1• Showing the obesity level under the BMI number.

• BMI > 32 = Obese

• 25 < BMI < 32 = Over Weight

• 18.5 < BMI < 25 = Normal Weight

• BMI < 18.5 = Under Weight

Page 35: [React Native Tutorial] Lecture 5: Input and State
Page 36: [React Native Tutorial] Lecture 5: Input and State

Making Stopwatch App!1. Draw UI on Paper

2. Initial project using react-native

1. $|> react-native init stopwatch

3. Making UI

4. Adding Style

5. Adding Logic

Page 37: [React Native Tutorial] Lecture 5: Input and State
Page 38: [React Native Tutorial] Lecture 5: Input and State
Page 39: [React Native Tutorial] Lecture 5: Input and State
Page 40: [React Native Tutorial] Lecture 5: Input and State

Initialize State

Page 41: [React Native Tutorial] Lecture 5: Input and State

Showing the timeElapse

Page 42: [React Native Tutorial] Lecture 5: Input and State

Handle Start Button Pressed

Page 43: [React Native Tutorial] Lecture 5: Input and State

Minutes-Second-Milliseconds Time Format

• Press Start Button • TimeElapsed in milliseconds is displayed. • We need to format TimeElapsed to

00:00.00 format, We have a library for that. • Go to terminal at the working directory

• >|$ npm install minutes-

seconds-milliseconds --save

Page 44: [React Native Tutorial] Lecture 5: Input and State

Minutes-Second-Milliseconds Time Format

Page 45: [React Native Tutorial] Lecture 5: Input and State

UI Separation• We need to make the Stop button first. (Only start is

showing now)

• It is a good idea to seperate the start/stop button UI from the main render method, since the main render method is very long (hard to read, and hard to maintenance)

Add startButton, stopButton style

Page 46: [React Native Tutorial] Lecture 5: Input and State

startStopButton method

Using startStopButton method

Page 47: [React Native Tutorial] Lecture 5: Input and State

Don’t forget the method binding

Page 48: [React Native Tutorial] Lecture 5: Input and State

Making Timer Stop

Page 49: [React Native Tutorial] Lecture 5: Input and State

Working Start/Stop Timer App

Page 50: [React Native Tutorial] Lecture 5: Input and State

Making Lap Function Works!

• Every time when user press the lap button, record the current timeElapsed into this.state.lap and reset the startTime

• Make the lower half of the app’s screen showing the list of timeElapsed in this.state.lap array.

Page 51: [React Native Tutorial] Lecture 5: Input and State

Implement handleLapPress

Page 52: [React Native Tutorial] Lecture 5: Input and State

Lap Button UI Separation

render method

Page 53: [React Native Tutorial] Lecture 5: Input and State

Display the Lap Array

Page 54: [React Native Tutorial] Lecture 5: Input and State
Page 55: [React Native Tutorial] Lecture 5: Input and State

See the difference

With the UI separation

Without

Page 56: [React Native Tutorial] Lecture 5: Input and State

Full Source Code# 1

Page 57: [React Native Tutorial] Lecture 5: Input and State

Full Source Code# 2

Page 58: [React Native Tutorial] Lecture 5: Input and State

Full Source Code# 3

Page 59: [React Native Tutorial] Lecture 5: Input and State

Full Source Code# 4

Page 60: [React Native Tutorial] Lecture 5: Input and State

Home Work

• How to reset the laps?

• It is leaved as the homework for you.

• See the iOS 9’s stopwatch app for idea.