Download - Hi Fiber!

Transcript
Page 1: Hi Fiber!

✋Hi Five Fiber!Lo que viene en React

Page 2: Hi Fiber!

Leonardo Garcia Crespo

(Lenny)

slack.meetupjs.com.ar

leogcrespo

leoasis

Page 3: Hi Fiber!

React hoy

Page 4: Hi Fiber!

Re-renderizar toda la app

Page 5: Hi Fiber!

class Message extends Component { render() { return <p>Hello {this.props.who}</p>; } }

ReactDOM.render(<Message who="World" />, mountNode); ReactDOM.render(<Message who="Planet" />, mountNode);

Page 6: Hi Fiber!

React decide cómo re-renderizar

Page 7: Hi Fiber!

class Increment extends Component { state = { value: 0 };

handleIncrement() { this.setState({ value: this.state.value + 1 }); }

render() { return ( <div> {this.state.value} <button onClick={this.handleIncrement.bind(this)}> +1 </button> </div> ); } }

Page 8: Hi Fiber!

React decide cuándo re-renderizar

Page 9: Hi Fiber!

Pero renderiza todo sincrónicamente

Page 10: Hi Fiber!

Problema para árboles de componentes grandes

Page 11: Hi Fiber!

Posibles optimizaciones

Page 12: Hi Fiber!

Priorizar actualizaciones

Page 13: Hi Fiber!

Qué actualización es más importante?

• Llegada de datos por AJAX

• Animaciones

• Dispatch de Redux

• Eventos causados por el usuario

Page 14: Hi Fiber!

Qué actualización es más importante?

• Llegada de datos por AJAX

• Animaciones

• Dispatch de Redux

• Eventos causados por el usuario

Page 15: Hi Fiber!

Ejecutar cuando sea más conveniente

Page 16: Hi Fiber!

• requestAnimationFrame

• requestIdleCallback

Page 17: Hi Fiber!

Ejecutar en partes (chunks)

Page 18: Hi Fiber!

• Pausar actualización si hay otra más importante

• Reanudar actualizaciones pausadas

• Abortar actualizaciones que ya no tienen sentido

• Reusar trabajo parcial anterior de otras actualizaciones

Page 19: Hi Fiber!

FiberAprovechando el modelo de React al mango

Page 20: Hi Fiber!

Qué es Fiber?O, cómo parte el trabajo React?

Page 21: Hi Fiber!

function work(arg1, arg2) { let i = 0; while (i < 1000) i++; //Hacemos algo // ... const fooRes = foo(arg1); const barRes = bar(arg2);

return baz(fooRes, barRes); }

Page 22: Hi Fiber!

function Work(props) { let i = 0; while (i < 1000) i++; //Hacemos algo // ... const fooRes = <Foo arg1={props.arg1} />; const barRes = <Bar arg2={props.arg2} />;

return <Baz>{fooRes}{barRes}</Baz>; }

Page 23: Hi Fiber!

function Work(props) { let i = 0; while (i < 1000) i++; //Hacemos algo // ... const fooRes = React.createElement(Foo, { arg1: props.arg1 }); const barRes = React.createElement(Bar, { arg2: props.arg2 });

return React.createElement(Baz, null, fooRes, barRes); }

Page 24: Hi Fiber!

Y si pudiéramos reimplementar el call stack?

Page 25: Hi Fiber!

Fiber, “virtual stack frame”

Page 26: Hi Fiber!

Stack frame Fiber (virtual stack frame)

Función Componente

Sub funciones children

Dirección de retorno Componente Padre

Parámetros props

Valor de retorno Cambios en el DOM

Page 27: Hi Fiber!

• Unidad de concurrencia

• “Virtual call stack” especializado para UIs

• No interactuamos con Fibers a nivel API en React (detalle de implementacion)

Page 28: Hi Fiber!

React mañana

Page 29: Hi Fiber!

Mejor performance con scheduling y concurrencia

Page 30: Hi Fiber!

Misma API*

Page 31: Hi Fiber!

Cambios semánticos en el ciclo de vida

Page 32: Hi Fiber!

• componentWillMount, componentWillUpdate, componentWillReceiveProps NO deben tener side effects.

• componentWillReceiveProps puede tener setState, pero solo sobre la misma instancia

• componentDidMount, componentDidUpdate y componentWillUnmount son los UNICOS que permiten side effects

Page 33: Hi Fiber!

Nueva API para especificar prioridades (TBD)

Page 34: Hi Fiber!

Demo: Sierpinksy Triangle

Page 35: Hi Fiber!

Nuevos features

Page 36: Hi Fiber!

Render de strings y numbers

Page 37: Hi Fiber!

function Strings(props) { return `Hello, ${props.world}`; }

class Double extends Component { render() { return this.props.number * 2; } }

Page 38: Hi Fiber!

Render de arrays

Page 39: Hi Fiber!

function Items() { return [ <li>Item 1</li>, <li>Item 2</li>, <li>Item 3</li>, ] }

Page 40: Hi Fiber!

Portals

Page 41: Hi Fiber!

class CreateThing extends Component { state = { showConfirmation: false };

render() { return ( <button onClick={() => this.setState({ showConfirmation: true }) }> Create Thing </button> <Modal isOpen={this.state.showConfirmation} title="Create Thing" content="Are you sure?" /> ) } }

Page 42: Hi Fiber!

const node = document.getElementById('modal');

function ModalContent(props) { return ( <div> <h1>{props.title}</h1> <p>{props.content}</p> </div> ); }

function Modal(props) { return ReactDOM.unstable_createPortal( props.isOpen && ( <ModalContent title={props.title} content={props.content} /> ), node ); }

Page 43: Hi Fiber!

Error boundaries

Page 44: Hi Fiber!

try catch para componentes

Page 45: Hi Fiber!

try { renderChildren(); } catch(e) { handleError(e); renderChildren(); }

Page 46: Hi Fiber!

Demo

Page 47: Hi Fiber!

Co-routines

Page 48: Hi Fiber!

Intercomunicación entre padre e hijo

Page 49: Hi Fiber!

Demo de Co-routines

Page 50: Hi Fiber!

Cuándo?

Page 51: Hi Fiber!

Primero como reemplazo de implementación, sincrónico, manteniendo feature parity

Page 52: Hi Fiber!

Luego con modo “incremental” (Scheduling y concurrencia)

Page 53: Hi Fiber!

Links

• React Design Principles

• Fiber Principles: Contributing To Fiber

• React Fiber Architecture

• Andrew Clark: What's Next for React — ReactNext 2016

Page 54: Hi Fiber!

React pasado mañana

Page 55: Hi Fiber!
Page 56: Hi Fiber!
Page 57: Hi Fiber!
Page 58: Hi Fiber!
Page 59: Hi Fiber!
Page 60: Hi Fiber!
Page 61: Hi Fiber!
Page 62: Hi Fiber!
Page 63: Hi Fiber!

Nuevo lenguaje?

Page 64: Hi Fiber!

https://facebook.github.io/reason/

ReasonML?

Page 65: Hi Fiber!

Gracias.