SlideShare a Scribd company logo
React: JSX and Top Level API
Sviluppare applicazioni client, server e mobile con React
Jiayi Hu, Maksim Sinik, Michele Stieven, Fabio Biondi
FABIO BIONDI
FREELANCE
fabiobiondi.io
EX FLASH-FLEX-AIR

CERTIFIED INSTRUCTOR :°(
2002-2012
I also love: Typescript, Redux, RxJS, D3.js, CreateJS,Arduino, ….
ANGULAR & REACT
DEVELOPER &TRAINER
COMMUNITIES
ANGULAR 

DEVELOPER ITALIANI
JAVASCRIPT 

DEVELOPER ITALIANI
REACT 

DEVELOPER ITALIANI
React: JSX and Top Level API
PARTNERS
ONE YEAR SUBSCRIPTION
(for 2 attendees)
Javascript ES6 : La guida mancante in italiano
Corso completo di sviluppo web: crea da zero il tuo business
La Fabbrica di innovazione e sviluppo tecnologico
dei servizi core per le aziende.
INutile è anche il tuo successo professionale dei prossimi anni 

www.inattivo.com
Progetto INutile
• Il marketplace che conne0e aziende e freelancer digitali
• Usa l’Ar:ficial Intelligence per la creazione di team di freelancer
• Un workspace Agile per la ges:one della collaborazione azienda - team
• Il primo marketplace collegato ad un ufficio 3D in Virtual Reality
• Iscrivi: alla newsle0er su h0ps://coderblock.com o sulla pagina facebook per
testare la pia0aforma in closed beta e ricevere i primi premi!
Il recruitment ai tempi del remote working.
IL PROGRAMMA DI OGGI
// 09:50-10:30

(JsxTopLevelAPI) => <FabioBiondi />



// 10:30-11:10

(ReactNative) => <MicheleStieven />



// 11:10-11:40

(coffee) => ‘networking’
// 11:40-12:20

(NextJS) => <MaksimSinik />



// 12:20-13:00

(customRenderer) => <JiayiHu />



<JSX>
Top-Level API
vs
Fabio Biondi
JSX
• Many Angular / Javascript developers 

think JSX is sh*t
• Why? Because of the HTML, CSS and JS “mix”

(in the same file)
JSX
In my opinion 

JSX is one of the most interesting features in React 

thanks to its flexibility
JSX
• JSX is a preprocessor step that adds XML syntax to JavaScript
• So, you can mix HTML, JS and CSS
• A simple way to update dynamically DOM
• You can definitely use React without JSX (thanks to React API) 

(but JSX makes React a lot more elegant)
GOAL
Component-based approach
<Widget> should contain several contents: 

React Components or HTML element
PART 1
JSXVS REACT TOP-LEVEL API
COMPONENTS: JSX
import React from ‘react’;

export const Text = () => {
return <div style={{color: 'red'}}>Hello</div>;
};
<Text />
HOWTO USE:
COMPONENT:
<div>Hello</div>
OUTPUT:
COMPONENT:
COMPONENTS: REACT API
export const Text = () => {
return React.createElement(
'div',
{
style: { color: 'green' }
},
'Hello');
};
<Text />
HOWTO USE:
<div>Hello</div>
OUTPUT:
DYNAMIC CONTENT: JSX
export const Text = props => {
return <div>{props.children}</div>
};
export const Home = () => {
return <Text>Hello World</Text>
};
HOWTO USE:
COMPONENT:
DYNAMIC CONTENT: API
export const Home = () => {
return <Text>Hello World</Text>
};
HOWTO USE:
COMPONENT:
export const Text = props => {
return React.createElement('div', null, props.children);
};
REAL WORLD EXAMPLE
<Text>React</Text>
<Text inline>Hello</Text>
<Text inline>World</Text>
COMPONENT:export const Text = props => {
const type = props.inline ? 'span' : 'div';
return React.createElement(
type, null, props.children
);
};
OUTPUT: DEVTOOLS
PART 2
<Widget /> component
<Dashboard> <Widget>
COMPONENT:
export const Dashboard = () => {
return (
<div>
<Widget title=“Temperatures” />
<Widget title=“Profile” />
<Widget title=“Anything" />
</div>
)
};
1/2
COMPONENT:
export const Dashboard = () => {
const destroyWidget = () => console.log(‘destroy widget');
const toggleOptions = () => console.log(‘toggle options’);


return (
<div>
<Widget
title="Temperatures"
onClose={() => destroyWidget()}
onToggleOptions={() => toggleOptions()} />
</div>
)
<Dashboard> <Widget> 2/2
FLOW
CREATE A CARD / WIDGET
IN BOOTSTRAP 4
COMPONENT:
<div class="card">
<div class="card-header">Widget Title</div>
<div class="card-body">
Widget Content
</div>
</div>
<Widget />
COMPONENT:
export const Widget = props => {
return (
<div className="card">
<div className="card-header">
<span>{props.title}</span>
<i className="fa fa-gear"
onClick={props.onToggleOptions}></i>
<i className="fa fa-times"
onClick={props.onClose}></i>
</div>
<div className="card-body">
...Widget Content...
</div>
</div>
)
}
PART 3
SPLIT IN COMPONENTS
COMPONENT TREE 1/2
COMPONENT TREE 2/2
<Widget> <WidgetBar>
COMPONENT:
export const Widget = props => {
return (
<div className="card">
<WidgetBar
title={props.title}
onToggleOptions={props.onToggleOptions}
onClose={props.onClose}
/>
<div className="card-body">
Widget Body
</div>
</div>
)
};
COMPONENT:
const WidgetBar = props => {
return (
<div className="card-header">
<span>{props.title}</span>
<i className="fa fa-gear pull-right"
onClick={props.onToggleOptions} />
<i className="fa fa-times pull-right"
onClick={props.onClose} />
</div>
)
}
<WidgetBar>
PART 4

<WidgetBody>
type AS CLASS NAME
<Widget title="Temperatures"
type={Chart}
config={temperatures} />


<Widget title=“User Profile"
type={Profile}
userID="123"/>
NOTE: each widget needs different properties to work
WidgetBody Content
const Chart = props => (
<h1 className=“m-2">
I'm a chart {props.config.data.length}
</h1>
);
const Profile = props => (
<div className="m-2">
<input type="text" defaultValue={props.userID}/>
...
</div>
);
const Any = props => `I’m another component`;
COMPONENT:
export const Widget = props => {
return (
<div className="card">
<WidgetBar
title={props.title}
onToggleOptions={props.onToggleOptions}
onClose={props.onClose}
/>
<WidgetBody {...props} />
</div>
)
}; NOTE: we use spread operator … to pass all props to a component
<Widget> <WidgetBody>
<WidgetBody>
const WidgetBody = props => {
const {
type, title, onToggleOptions, onClose, ...others
} = props;


return React.createElement(type, {...others}, null);
}
NOTE: we use ES6 destructuring
React: JSX and Top Level API
PART 5
type AS STRING
(USEFUL WHEN WIDGET CONFIGURATION

IS LOADED FROM JSON)
HOWTO USE
COMPONENT:
<Widget title=“Temperatures"
type="Chart"
config={myData} 

onClose={() => destroyWidget()}
onShowOptions={() => openOptions()} />
<Widget title="MyProfile"
type="Profile"
user=“123"

onClose={() => destroyWidget()}
onShowOptions={() => openOptions()} />
<Widget title=“Any Component"
type="Any"/>
or by using .map()
COMPONENT:
props.widgets.map(item => (
<Widget
key={item.id}
{...item}
onClose={() => destroyWidget()}
onShowOptions={() => openOptions()}
/>

);
<widgetBody>: JSX version
COMPONENT:
const WidgetBody = props => {
switch(props.type) {
case 'Chart': return <Chart {...props} />;
case 'Profile': return <Profile {...props} />;
case 'Any': return <Any {...props} />;
default: return <h3>You forgot type</h3>;
}
};
<WidgetBody>: REACT API/eval version
COMPONENT:
const WidgetBody = props => {
const { type, ...others } = props;


return React.createElement( eval(type), {...others}, null )
}
If you run eval() someone could run malicious code on the user's machine
<widgetBody>: API & Map() version
COMPONENT:
const WidgetBody = props => {
const { type, ...others } = props;
return React.createElement(

components.get(type), {...others}, null
)
}
const components = new Map();
components.set('Chart', Chart);
components.set('Profile', Profile);
components.set('Any', Any);
React: JSX and Top Level API
CAN I DOTHE SAME 

IN ANGULAR?


CREATING A DIRECTIVE:

<div
[widgetBody]=“myComponent”
(close)=“destroy()”
(toggleOptions)=“toggleMenu()”>
import {
ComponentRef, ComponentFactoryResolver, Directive, ElementRef, Input, OnDestroy, ViewContainerRef
} from '@angular/core';

@Directive({
selector: '[widgetBody]'
})
export class WidgetBodyDirective implements OnDestroy {
@Input() set widgetBody(component: WidgetComponent) {
this.view.clear();
if (component) {
let factory = this.resolver.resolveComponentFactory(component.type);
this.ref = this.view.createComponent(factory);
for (let key in component.payload) {
this.ref.instance[key] = component.payload[key];
}
}
}

private ref: ComponentRef<any>;
constructor( private view: ViewContainerRef, private resolver: ComponentFactoryResolver) { }
ngOnDestroy() {
if (this.ref) {
this.ref.destroy();
}
}
}
React: JSX and Top Level API
<source-code>
THANKS
fabiobiondi.io
JOIN OUR COMMUNITIES
ANGULAR 

DEVELOPER ITALIANI
JAVASCRIPT 

DEVELOPER ITALIANI
REACT 

DEVELOPER ITALIANI

More Related Content

What's hot (20)

PDF
Express: A Jump-Start
Naveen Pete
 
PDF
The productive developer guide to Angular 2
Maurice De Beijer [MVP]
 
PDF
React Native +Redux + ES6 (Updated)
Chiew Carol
 
PPTX
Angular js 2
Ran Wahle
 
PDF
Fundamental concepts of react js
StephieJohn
 
PDF
RESTful services and OAUTH protocol in IoT
Yakov Fain
 
PPTX
Angular 2 Migration - JHipster Meetup 6
William Marques
 
PDF
Intro To React Native
FITC
 
PPTX
Angular 5
Bartłomiej Narożnik
 
PDF
Angular 2: core concepts
Codemotion
 
PDF
Ngrx meta reducers
Eliran Eliassy
 
PDF
How to React Native
Dmitry Ulyanov
 
PDF
React js t4 - components
Jainul Musani
 
PPTX
AngularJS: Service, factory & provider
Corley S.r.l.
 
PDF
Serverless Angular, Material, Firebase and Google Cloud applications
Loiane Groner
 
PDF
Putting the Native in React Native - React Native Boston
stan229
 
PPTX
React render props
Saikat Samanta
 
PDF
Angular2 - In Action
Sebastian Pożoga
 
PDF
Introduction to react native
Dani Akash
 
PPTX
Code migration from Angular 1.x to Angular 2.0
Ran Wahle
 
Express: A Jump-Start
Naveen Pete
 
The productive developer guide to Angular 2
Maurice De Beijer [MVP]
 
React Native +Redux + ES6 (Updated)
Chiew Carol
 
Angular js 2
Ran Wahle
 
Fundamental concepts of react js
StephieJohn
 
RESTful services and OAUTH protocol in IoT
Yakov Fain
 
Angular 2 Migration - JHipster Meetup 6
William Marques
 
Intro To React Native
FITC
 
Angular 2: core concepts
Codemotion
 
Ngrx meta reducers
Eliran Eliassy
 
How to React Native
Dmitry Ulyanov
 
React js t4 - components
Jainul Musani
 
AngularJS: Service, factory & provider
Corley S.r.l.
 
Serverless Angular, Material, Firebase and Google Cloud applications
Loiane Groner
 
Putting the Native in React Native - React Native Boston
stan229
 
React render props
Saikat Samanta
 
Angular2 - In Action
Sebastian Pożoga
 
Introduction to react native
Dani Akash
 
Code migration from Angular 1.x to Angular 2.0
Ran Wahle
 

Similar to React: JSX and Top Level API (20)

PDF
Introduction to React for Frontend Developers
Sergio Nakamura
 
PDF
React Native for multi-platform mobile applications
Matteo Manchi
 
PDF
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
Luciano Mammino
 
PPTX
React & Redux for noobs
[T]echdencias
 
PDF
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Ontico
 
PPTX
How to create components in react js
BOSC Tech Labs
 
PPTX
Angular2 + rxjs
Christoffer Noring
 
PDF
A Blog About Using State in Next.js: Valuable Insights
Shiv Technolabs Pvt. Ltd.
 
PPTX
React native by example by Vadim Ruban
Lohika_Odessa_TechTalks
 
PDF
React redux
Michel Perez
 
PDF
Building Universal Web Apps with React ForwardJS 2017
Elyse Kolker Gordon
 
PDF
Full Stack React Workshop [CSSC x GDSC]
GDSC UofT Mississauga
 
PPTX
React 101 by Anatoliy Sieryi
Binary Studio
 
PDF
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Codemotion
 
PDF
Universal JS Web Applications with React - Luciano Mammino - Codemotion Rome ...
Luciano Mammino
 
PDF
Mastering JavaScript and DOM: A Gateway to Web Development
Piyumi Niwanthika Herath
 
PDF
2018 05-16 Evolving Technologies: React, Babel & Webpack
Codifly
 
PDF
Building a js widget
Tudor Barbu
 
PDF
Lessons from a year of building apps with React Native
Ryan Boland
 
PPTX
React outbox
Angela Lehru
 
Introduction to React for Frontend Developers
Sergio Nakamura
 
React Native for multi-platform mobile applications
Matteo Manchi
 
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
Luciano Mammino
 
React & Redux for noobs
[T]echdencias
 
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Ontico
 
How to create components in react js
BOSC Tech Labs
 
Angular2 + rxjs
Christoffer Noring
 
A Blog About Using State in Next.js: Valuable Insights
Shiv Technolabs Pvt. Ltd.
 
React native by example by Vadim Ruban
Lohika_Odessa_TechTalks
 
React redux
Michel Perez
 
Building Universal Web Apps with React ForwardJS 2017
Elyse Kolker Gordon
 
Full Stack React Workshop [CSSC x GDSC]
GDSC UofT Mississauga
 
React 101 by Anatoliy Sieryi
Binary Studio
 
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Codemotion
 
Universal JS Web Applications with React - Luciano Mammino - Codemotion Rome ...
Luciano Mammino
 
Mastering JavaScript and DOM: A Gateway to Web Development
Piyumi Niwanthika Herath
 
2018 05-16 Evolving Technologies: React, Babel & Webpack
Codifly
 
Building a js widget
Tudor Barbu
 
Lessons from a year of building apps with React Native
Ryan Boland
 
React outbox
Angela Lehru
 
Ad

More from Fabio Biondi (18)

PDF
Redux Toolkit - Quick Intro - 2022
Fabio Biondi
 
PDF
React - Component Based Approach
Fabio Biondi
 
PDF
Introduction to E2E in Cypress
Fabio Biondi
 
PDF
Create your React 18 / TS bundle using esbuild
Fabio Biondi
 
PDF
Create Web Components using Google Lit
Fabio Biondi
 
PDF
Redux Toolkit & RTK Query in TypeScript: tips&tricks
Fabio Biondi
 
PDF
React Typescript for beginners: Translator app with Microsoft cognitive services
Fabio Biondi
 
PDF
RXJS Best (& Bad) Practices for Angular Developers
Fabio Biondi
 
PDF
Introduction for Master Class "Amazing Reactive Forms"
Fabio Biondi
 
PDF
Data architectures in Angular & NGRX Introduction
Fabio Biondi
 
PDF
RxJS & Angular Reactive Forms @ Codemotion 2019
Fabio Biondi
 
PDF
Angular & RXJS: examples and use cases
Fabio Biondi
 
PDF
Angular Day 2018 (italy) - Keynote - The Amazing World of Angular 6
Fabio Biondi
 
PDF
Introduction to Redux (for Angular and React devs)
Fabio Biondi
 
PDF
Angular Best Practices @ Firenze 19 feb 2018
Fabio Biondi
 
PDF
Intro evento: evolvere un applicazione Angular con Rxjs e Redux
Fabio Biondi
 
PDF
Single Page Applications in Angular (italiano)
Fabio Biondi
 
PDF
Angular 2 - Core Concepts
Fabio Biondi
 
Redux Toolkit - Quick Intro - 2022
Fabio Biondi
 
React - Component Based Approach
Fabio Biondi
 
Introduction to E2E in Cypress
Fabio Biondi
 
Create your React 18 / TS bundle using esbuild
Fabio Biondi
 
Create Web Components using Google Lit
Fabio Biondi
 
Redux Toolkit & RTK Query in TypeScript: tips&tricks
Fabio Biondi
 
React Typescript for beginners: Translator app with Microsoft cognitive services
Fabio Biondi
 
RXJS Best (& Bad) Practices for Angular Developers
Fabio Biondi
 
Introduction for Master Class "Amazing Reactive Forms"
Fabio Biondi
 
Data architectures in Angular & NGRX Introduction
Fabio Biondi
 
RxJS & Angular Reactive Forms @ Codemotion 2019
Fabio Biondi
 
Angular & RXJS: examples and use cases
Fabio Biondi
 
Angular Day 2018 (italy) - Keynote - The Amazing World of Angular 6
Fabio Biondi
 
Introduction to Redux (for Angular and React devs)
Fabio Biondi
 
Angular Best Practices @ Firenze 19 feb 2018
Fabio Biondi
 
Intro evento: evolvere un applicazione Angular con Rxjs e Redux
Fabio Biondi
 
Single Page Applications in Angular (italiano)
Fabio Biondi
 
Angular 2 - Core Concepts
Fabio Biondi
 
Ad

Recently uploaded (20)

PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPT
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Tally software_Introduction_Presentation
AditiBansal54083
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 

React: JSX and Top Level API

  • 2. Sviluppare applicazioni client, server e mobile con React Jiayi Hu, Maksim Sinik, Michele Stieven, Fabio Biondi
  • 5. I also love: Typescript, Redux, RxJS, D3.js, CreateJS,Arduino, …. ANGULAR & REACT DEVELOPER &TRAINER
  • 7. ANGULAR 
 DEVELOPER ITALIANI JAVASCRIPT 
 DEVELOPER ITALIANI REACT 
 DEVELOPER ITALIANI
  • 11. Javascript ES6 : La guida mancante in italiano Corso completo di sviluppo web: crea da zero il tuo business
  • 12. La Fabbrica di innovazione e sviluppo tecnologico dei servizi core per le aziende. INutile è anche il tuo successo professionale dei prossimi anni 
 www.inattivo.com Progetto INutile
  • 13. • Il marketplace che conne0e aziende e freelancer digitali • Usa l’Ar:ficial Intelligence per la creazione di team di freelancer • Un workspace Agile per la ges:one della collaborazione azienda - team • Il primo marketplace collegato ad un ufficio 3D in Virtual Reality • Iscrivi: alla newsle0er su h0ps://coderblock.com o sulla pagina facebook per testare la pia0aforma in closed beta e ricevere i primi premi! Il recruitment ai tempi del remote working.
  • 15. // 09:50-10:30
 (JsxTopLevelAPI) => <FabioBiondi />
 
 // 10:30-11:10
 (ReactNative) => <MicheleStieven />
 
 // 11:10-11:40
 (coffee) => ‘networking’ // 11:40-12:20
 (NextJS) => <MaksimSinik />
 
 // 12:20-13:00
 (customRenderer) => <JiayiHu />
 

  • 17. JSX • Many Angular / Javascript developers 
 think JSX is sh*t • Why? Because of the HTML, CSS and JS “mix”
 (in the same file)
  • 18. JSX In my opinion 
 JSX is one of the most interesting features in React 
 thanks to its flexibility
  • 19. JSX • JSX is a preprocessor step that adds XML syntax to JavaScript • So, you can mix HTML, JS and CSS • A simple way to update dynamically DOM • You can definitely use React without JSX (thanks to React API) 
 (but JSX makes React a lot more elegant)
  • 20. GOAL
  • 22. <Widget> should contain several contents: 
 React Components or HTML element
  • 23. PART 1 JSXVS REACT TOP-LEVEL API
  • 24. COMPONENTS: JSX import React from ‘react’;
 export const Text = () => { return <div style={{color: 'red'}}>Hello</div>; }; <Text /> HOWTO USE: COMPONENT: <div>Hello</div> OUTPUT:
  • 25. COMPONENT: COMPONENTS: REACT API export const Text = () => { return React.createElement( 'div', { style: { color: 'green' } }, 'Hello'); }; <Text /> HOWTO USE: <div>Hello</div> OUTPUT:
  • 26. DYNAMIC CONTENT: JSX export const Text = props => { return <div>{props.children}</div> }; export const Home = () => { return <Text>Hello World</Text> }; HOWTO USE: COMPONENT:
  • 27. DYNAMIC CONTENT: API export const Home = () => { return <Text>Hello World</Text> }; HOWTO USE: COMPONENT: export const Text = props => { return React.createElement('div', null, props.children); };
  • 28. REAL WORLD EXAMPLE <Text>React</Text> <Text inline>Hello</Text> <Text inline>World</Text> COMPONENT:export const Text = props => { const type = props.inline ? 'span' : 'div'; return React.createElement( type, null, props.children ); };
  • 30. PART 2 <Widget /> component
  • 31. <Dashboard> <Widget> COMPONENT: export const Dashboard = () => { return ( <div> <Widget title=“Temperatures” /> <Widget title=“Profile” /> <Widget title=“Anything" /> </div> ) }; 1/2
  • 32. COMPONENT: export const Dashboard = () => { const destroyWidget = () => console.log(‘destroy widget'); const toggleOptions = () => console.log(‘toggle options’); 
 return ( <div> <Widget title="Temperatures" onClose={() => destroyWidget()} onToggleOptions={() => toggleOptions()} /> </div> ) <Dashboard> <Widget> 2/2
  • 33. FLOW
  • 34. CREATE A CARD / WIDGET IN BOOTSTRAP 4 COMPONENT: <div class="card"> <div class="card-header">Widget Title</div> <div class="card-body"> Widget Content </div> </div>
  • 35. <Widget /> COMPONENT: export const Widget = props => { return ( <div className="card"> <div className="card-header"> <span>{props.title}</span> <i className="fa fa-gear" onClick={props.onToggleOptions}></i> <i className="fa fa-times" onClick={props.onClose}></i> </div> <div className="card-body"> ...Widget Content... </div> </div> ) }
  • 36. PART 3 SPLIT IN COMPONENTS
  • 39. <Widget> <WidgetBar> COMPONENT: export const Widget = props => { return ( <div className="card"> <WidgetBar title={props.title} onToggleOptions={props.onToggleOptions} onClose={props.onClose} /> <div className="card-body"> Widget Body </div> </div> ) };
  • 40. COMPONENT: const WidgetBar = props => { return ( <div className="card-header"> <span>{props.title}</span> <i className="fa fa-gear pull-right" onClick={props.onToggleOptions} /> <i className="fa fa-times pull-right" onClick={props.onClose} /> </div> ) } <WidgetBar>
  • 42. type AS CLASS NAME <Widget title="Temperatures" type={Chart} config={temperatures} /> 
 <Widget title=“User Profile" type={Profile} userID="123"/> NOTE: each widget needs different properties to work
  • 43. WidgetBody Content const Chart = props => ( <h1 className=“m-2"> I'm a chart {props.config.data.length} </h1> ); const Profile = props => ( <div className="m-2"> <input type="text" defaultValue={props.userID}/> ... </div> ); const Any = props => `I’m another component`;
  • 44. COMPONENT: export const Widget = props => { return ( <div className="card"> <WidgetBar title={props.title} onToggleOptions={props.onToggleOptions} onClose={props.onClose} /> <WidgetBody {...props} /> </div> ) }; NOTE: we use spread operator … to pass all props to a component <Widget> <WidgetBody>
  • 45. <WidgetBody> const WidgetBody = props => { const { type, title, onToggleOptions, onClose, ...others } = props; 
 return React.createElement(type, {...others}, null); } NOTE: we use ES6 destructuring
  • 47. PART 5 type AS STRING (USEFUL WHEN WIDGET CONFIGURATION
 IS LOADED FROM JSON)
  • 48. HOWTO USE COMPONENT: <Widget title=“Temperatures" type="Chart" config={myData} 
 onClose={() => destroyWidget()} onShowOptions={() => openOptions()} /> <Widget title="MyProfile" type="Profile" user=“123"
 onClose={() => destroyWidget()} onShowOptions={() => openOptions()} /> <Widget title=“Any Component" type="Any"/>
  • 49. or by using .map() COMPONENT: props.widgets.map(item => ( <Widget key={item.id} {...item} onClose={() => destroyWidget()} onShowOptions={() => openOptions()} />
 );
  • 50. <widgetBody>: JSX version COMPONENT: const WidgetBody = props => { switch(props.type) { case 'Chart': return <Chart {...props} />; case 'Profile': return <Profile {...props} />; case 'Any': return <Any {...props} />; default: return <h3>You forgot type</h3>; } };
  • 51. <WidgetBody>: REACT API/eval version COMPONENT: const WidgetBody = props => { const { type, ...others } = props; 
 return React.createElement( eval(type), {...others}, null ) } If you run eval() someone could run malicious code on the user's machine
  • 52. <widgetBody>: API & Map() version COMPONENT: const WidgetBody = props => { const { type, ...others } = props; return React.createElement(
 components.get(type), {...others}, null ) } const components = new Map(); components.set('Chart', Chart); components.set('Profile', Profile); components.set('Any', Any);
  • 54. CAN I DOTHE SAME 
 IN ANGULAR?
  • 56. import { ComponentRef, ComponentFactoryResolver, Directive, ElementRef, Input, OnDestroy, ViewContainerRef } from '@angular/core';
 @Directive({ selector: '[widgetBody]' }) export class WidgetBodyDirective implements OnDestroy { @Input() set widgetBody(component: WidgetComponent) { this.view.clear(); if (component) { let factory = this.resolver.resolveComponentFactory(component.type); this.ref = this.view.createComponent(factory); for (let key in component.payload) { this.ref.instance[key] = component.payload[key]; } } }
 private ref: ComponentRef<any>; constructor( private view: ViewContainerRef, private resolver: ComponentFactoryResolver) { } ngOnDestroy() { if (this.ref) { this.ref.destroy(); } } }
  • 60. JOIN OUR COMMUNITIES ANGULAR 
 DEVELOPER ITALIANI JAVASCRIPT 
 DEVELOPER ITALIANI REACT 
 DEVELOPER ITALIANI