Top Banner
Elio Struyf Trainer @ U2U – MVP October 25th, 2017 Getting up to speed with React
42

SPUnite17 Getting up to Speed with React

Jan 28, 2018

Download

Technology

NCCOMMS
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SPUnite17 Getting up to Speed with React

ElioStruyfTrainer@U2U– MVPOctober25th,2017

GettinguptospeedwithReact

Page 2: SPUnite17 Getting up to Speed with React
Page 3: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Abriefmomenttothepast

Page 4: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

DOMManipulation

HTML JavaScript

Page 5: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Databinding

HTML JavaScript

var app = angular.module('feedbackApp', []);app.controller('feedbackController', ['$scope', '$http', ($scope, $http) => {

$scope.oneAtATime = true;

$http.get('tracks.json?ver=0.1').success(data => {$scope.tracks = data;

});}]

);

<body data-ng-app="feedbackApp" data-ng-controller="feedbackController"><ul id="tracks"> <li data-ng-repeat="track in tracks" class="track"><h2>{{track.title}}</h2><ul class="trackSessions"><li data-ng-repeat="session in track.sessions"

class="session"><div class="sessionDetails"><a data-ng-href="{{session.url}}">{{session.title}}</a><span ng-if="session.speaker">by {{session.speaker}}

({{session.start}} - {{session.end}})</span><span class="note" ng-

if="session.note">{{session.note}}</span></div>

</li></ul>

</li></ul>

</body>

Page 6: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

ReactisaJavaScriptlibraryforbuildinguserinterfaces

Page 7: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Components

Page 8: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Componentbased

Page 9: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Reactdoesnotseparatecomponenttemplates

Page 10: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

YouwriteHTMLinTypeScript

Page 11: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

JSXandTSX

AddsXMLsyntaxtoJavaScript/TypeScript

<a href="https://www.eliostruyf.com" title="Elio Struyf">My blog</a>

React.createElement("a", { href: "https://www.eliostruyf.com", title: "Elio Struyf" }, "My blog")

JSX

PlainJavaScript

Page 12: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

VariablesinJSX/TSX

<a href={blogLink.url} title={blogLink.title}>{blogLink.text}</a>

React.createElement("a", { href: blogLink.url, title: blogLink.title }, blogLink.text)

PlainJavaScript

JSX

Page 13: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Simplecomponent

class ComponentName extends React.Component<{}, {}> {public render() {

return (<div>

Component name should start with a capital letter</div>

);}

}

Page 14: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Usingthecomponentcomponent

<ComponentName />

Page 15: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Events

Page 16: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Usingthecomponentcomponent

class Sample extends React.Component<{}, {}> {private _linkClick() {console.log('Hey, you clicked me!');

}

public render(): React.ReactElement<{}> {return (<a href="javascript:;" onClick={this._linkClick}>Click me!</a>

);}

}

Page 17: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Howdoweconvertthis?

Page 18: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Youdon’thavetoworryaboutit.InSPFx everythingisalreadyinplace.

Page 19: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

JSXlimitations

Page 20: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

SomekeywordsarereservedinJS/TS

• classà className• colspanà colSpan• innerHTMLà dangerouslySetInnerHTML• forà htmlFor• styleà style(itisanobject!)

Page 21: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Demo:Component

Page 22: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Inputs(props)andstate

Page 23: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Inputpropertiesorprops

• Passinginformationtoyourcomponent• Donotupdatepropertiesinyourcomponent• Usedforrenderingorcalculations

<div>Description: {this.props.description}

</div>

Page 24: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Passingpropstocomponents

<ComponentName description="This is the description" />

Page 25: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Componentstateorstate

• Keepholdofthecurrentcomponentsvariables• Consideredprivatetothecomponent

constructor(props: IProps) {super(props);

// initialize the statethis.state = {

show: false};

}

Page 26: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Updatethecomponentstate

• UpdatestatebyusingsetState• Thinkofitasarequest ratherthananimmediatecommand• Forbetterperformance,Reactmaydelayit

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

show: !prevState.show};

});

Page 27: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Demo:Workingwith

propsandstates

Page 28: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Listingitems

Page 29: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Importantthingswhenlistingitems

• Alwaysuseakey• Trytoavoidusingthenitemindex

public render() {const itemList = [{ id: 0, title: "Hello" }, { id: 2, title: "," },

{ id: 4, title: "World" }];return (<ul> {itemList.map(item => {return <li key={item.id}>{item.title}</li>;

}) }</ul>

);}

Page 30: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

VirtualDOM

Page 31: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

VirtualDOMandchangedetectionTherealDOMVirtualDOM

Page 32: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Demo:Listingitems

Page 33: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Lifecyclehooks

Page 34: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Lifecyclehooks

• Mounting:• constructor,componentWillMount,componentDidMount

• Updating:• componentWillReceiveProps,shouldComponentUpdate,componentWillUpdate,componentDidUpdate

• Unmounting• componentWillUnmount

Page 35: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

CallingAPIsandstoringdata

Page 36: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Wheretocallfordata

• Donotdoasync callintheconstructor• UsethecomponentDidMount lifecyclehook

public componentDidMount(): void {this._getAsyncData().then(items => {

if (items) {this.setState({

items});

}});

}

Page 37: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Demo:Retrievingasync data

Page 38: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

WorkingwithSCSSinSPFx

Page 39: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf

Demo:Stylingyourcomponent

Page 40: SPUnite17 Getting up to Speed with React

Questions?

Page 41: SPUnite17 Getting up to Speed with React

Office Servers & Services MVPAzure / Office 365 / SharePoint

@[email protected]

ElioStruyfLead trainer and architect

Page 42: SPUnite17 Getting up to Speed with React

#SPUnite17- @eliostruyf