cs/coe 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfallows you to write...

26
CS/COE 1520 pitt.edu/~ach54/cs1520 React

Upload: others

Post on 03-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

CS/COE 1520pitt.edu/~ach54/cs1520

React

Page 2: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● class Square extends React.Component {

render() {

return (

<button className="square" onClick={

function() { alert('click'); }

}>

{this.props.value}

</button>

);

}

}

A terrible introduction to React:

2

???

???

Page 3: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Using DOM manipulation:

○ var element = document.createElement('h1');

element.setAttribute('class', 'greeting');

var tn = document.createTextNode('Hello, world!');

element.appendChild(tn);

● Using React:

○ const element = React.createElement('h1',

{className: 'greeting'},

'Hello, world!'

);

Creating new elements

3

Page 4: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● React.createElement() returns an object something like this:

○ { type: 'h1',

props: { className: 'greeting',

children: 'Hello, world!'

}

}

● Cannot be attached directly into the DOM hierarchy

○ Must be rendered using the ReactDOM

Well, not exactly...

4

Page 5: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● A "virtual DOM"

● Keeps a cache of data structures representing what the

rendered page should look like

● Compute differences with the displayed DOM, and issues the

minimal amount of actual DOM manipulation calls to reflect

the requested changes

ReactDOM

5

Page 6: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● ReactDOM.render(

element,

document.getElementById('targetDiv')

);

Using the ReactDOM

6

Page 7: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● element = <h1 className="greeting">Hello, world!</h1>;

● Syntax extensions to JavaScript

○ "Compiled" into React.createElement() calls

■ Hence, evaluate to JS objects, and can be used as such

● Can also embed JS expressions within JSX via {}:

○ const element = (

<h1>

Hello, {getName() + "!"}

</h1>;

);

JSX

7

Page 8: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● As we saw, can use string literals as params with ""

● Can also use JS expressions as params, wrapped with {}, do

not wrap the {} with "". E.g.:

○ element = <h1 className={getGreet()}>Hello, world!</h1>;

● React DOM uses camelCase property naming convention

instead of HTML attribute names

○ class is instead className

○ tabindex is tabIndex

JSX properties

8

Page 9: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Add this line to your page:

○ <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

● Can now use JSX in future invoked scripts by setting the type

attribute to type="text/babel"

○ Will use Babel to transpile JSX into compatible JavaScript!

"Compiling" JSX

9

Page 10: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Can use ReactDom.render() just like in the earlier example

● How do you update JSX elements?

○ You can't!

■ They are immutable

○ You can, however, generate a new element and render that in

place of the old one

■ E.g., by using the same targetDiv as the render root

Rendering JSX elements

10

Page 11: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● function Welcome(props) {

return <h1>Hello, {props.who}</h1>;

}

● const element = <Welcome who="world" />;

ReactDOM.render(

element,

document.getElementById('root')

);

Components

11

Page 12: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Think back to the proposed tutorial:

○ Building a tic-tac-toe board

● Need to keep track of whether or not a square on the board

has been clicked in order to render it

○ How can we maintain this state?

■ Define our Component using classes, not functions

Encapsulation

12

Page 13: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Well, it didn't…

○ Until ES6

But JavaScript doesn't have classes!

13

Page 14: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● class Person {

constructor(name, age) {

this.name = name;

this.age = age;

}

display() {

console.log("Name: " + this.name);

console.log("Age: " + this.age + "\n");

}

}

ES6 classes

14

Page 15: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Classes cannot be instantiated before their definition

○ I.e.., class definitions are not "hoisted"

● Class methods are not constructible

○ Cannot be used on the right of a new

● Can have generators as methods

○ Using *method_name()

● Class bodies are evaluated in strict mode

● All instance attributes must be defined in method bodies

ES6 class notes

15

Page 16: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Class properties must be set outside of the class body:

○ Person.species = "Homo sapiens";

● The static keyword can be used to define class methods

Class properties/methods

16

Page 17: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● class Student extends Person {

constructor(name, age) {

super(name, age);

this.classes = [];

}

add_class(new) {

this.classes.push(new);

}

display() {

super.display()

console.log("Classes: " + this.classes + "\n");

}

}

Inheritance

17

Page 18: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● class Rectangle {constructor(height, width) {

this.height = height;this.width = width;

}get area() {

return this.calcArea();}calcArea() {

return this.height * this.width;}

}● const square = new Rectangle(10, 10);● console.log(square.area); // 100

Getter/setter methods

18

Page 19: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Could also have defined this:

○ function Welcome(props) {

return <h1>Hello, {props.who}</h1>;

}

● As:

○ class Welcome extends React.Component {

render() {

return <h1>Hello, {this.props.who}</h1>;

}

}

Back to React...

19

Page 20: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● class Clock extends React.Component {constructor(props) {

super(props);this.state = {date: new Date()};

}render() {

return (<div>

<h1>Hello, world!</h1><h2>{this.state.date.toLocaleTimeString()}</h2>

</div>);

}}

● ReactDOM.render(<Clock />, document.getElementById('root'));

But now we can use the object to encapsulate state

20

Page 21: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Let's add these definitions:○ componentDidMount() {

this.timerID = setInterval(

() => this.tick(),

1000

);

}

componentWillUnmount() {

clearInterval(this.timerID);

}

tick() {

this.setState({ date: new Date() });

}

That's a boring clock...

21

Page 22: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Never directly modify the attributes of a components .state

○ Aside from the constructor

○ Call setState()

● Never reference the current state in a call to setState()

○ setState() calls may be performed asynchronously

○ setState() has a second form that accepts a function

■ First argument, cur state

■ Second argument, cur props

Modifying the state

22

Page 23: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Bad:

○ this.setState({

counter: this.state.counter + this.props.increment

});

● Good:

○ this.setState((prevState, props) => ({

counter: prevState.counter + props.increment

}));

Modifying state examples:

23

Page 24: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● See example

Event handling

24

Page 25: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Brought up when we introduced JavaScript

○ Acting along similar lines to separation of structure and

presentation from HTML/CSS

● React kind of does away with that…

○ Thoughts?

Separation of markup and logic

25

Page 26: CS/COE 1520pitt.edu/~ach54/teaching/cs1520-2201/files/slides/react.pdfAllows you to write Android/iOS apps using JS and React! Creates an app that will load a JS engine in a thread

● Allows you to write Android/iOS apps using JS and React!

● Creates an app that will load a JS engine in a thread and run

the React Native code that you write in that

● The UI you build via React Native will be rendered using

native UI elements to the platform that you are running on

(Android or iOS)

React Native

26