reactjs / flux - responsive web with ddd and event-driven design

31
ReactJS / FLUX Responsive web with DDD and event-driven design.

Upload: rinat-abdullin

Post on 12-Jul-2015

6.546 views

Category:

Software


1 download

TRANSCRIPT

Page 1: ReactJS / FLUX - Responsive web with DDD and event-driven design

ReactJS / FLUXResponsive web with DDD and event-driven design.

Page 2: ReactJS / FLUX - Responsive web with DDD and event-driven design

Format• ~1 hour talk

• ReactJS

• Facebook FLUX

• Isomorphic Web Apps

• Questions any time.

• Will show code.

• Open discussion afterwards

Page 3: ReactJS / FLUX - Responsive web with DDD and event-driven design
Page 4: ReactJS / FLUX - Responsive web with DDD and event-driven design

User Interface

• Ubiquitous language revolves around components.

• Components are focused and reusable.

• Our UI is composed from these components.

• Finding component boundaries is very similar to DDDesign process.

Page 5: ReactJS / FLUX - Responsive web with DDD and event-driven design

UI Behaviors

• Ubiquitous Language revolves around events.

• Events can originate from user interactions (TaskChecked) or from environment (FailedToSaveTaskToServer).

• Multiple components could react in response to a single Event.

Page 6: ReactJS / FLUX - Responsive web with DDD and event-driven design

Apply DDD to UI and compose it from reusable visual components aligned with the domain.

Capture UI interactions as functional event-driven behaviors.

Page 7: ReactJS / FLUX - Responsive web with DDD and event-driven design

ReactJSJS library from Facebook with ONE magical feature

Page 8: ReactJS / FLUX - Responsive web with DDD and event-driven design

Simple Componentvar TodoList = React.createClass({ render: function() { var createItem = function(itemText) { return <li>{itemText}</li>; }; return <ul>{this.props.items.map(createItem)}</ul>; }});

// ul and li are components, too

Page 9: ReactJS / FLUX - Responsive web with DDD and event-driven design

–ReactJS

“Don’t mutate UI! Re-render entire UI to Virtual DOM on every change!”

Page 10: ReactJS / FLUX - Responsive web with DDD and event-driven design
Page 11: ReactJS / FLUX - Responsive web with DDD and event-driven design

ReactJS Summary• Decompose UI into reusable components which

render to virtual DOM.

• ReactJS will sync VDOM to real browser DOM.

• JSX format is an optional syntactic sugar.

• ReactJS components encapsulate layout and behavior in one file.

• Can render on client and server.

Page 12: ReactJS / FLUX - Responsive web with DDD and event-driven design

UI CompositionAppPage LeftNavBar AgendaNavItem InboxNavItem StarredNavItem MainViewArea InboxView TaskList TaskItem TaskCheck TaskStar TaskItem TaskItem TaskComposer

— owns state — pure — pure — pure — pure — pure — owns state — pure — pure — pure — pure — pure — pure — owns state

Page 13: ReactJS / FLUX - Responsive web with DDD and event-driven design

Pure Componentvar TodoList = React.createClass({ render: function() { var createItem = function(itemText) { return <li>{itemText}</li>; }; return <ul>{this.props.items.map(createItem)}</ul>; }});

Page 14: ReactJS / FLUX - Responsive web with DDD and event-driven design

Statevar TodoApp = React.createClass({ getInitialState: function() { return {items: [], text: ''}; }, onChange: function(e) { this.setState({text: e.target.value}); }, handleSubmit: function(e) { e.preventDefault(); var nextItems = this.state.items.concat([this.state.text]); var nextText = ''; this.setState({items: nextItems, text: nextText}); }, render: function() { return ( <div> <h3>TODO</h3> <TodoList items={this.state.items} /> <form onSubmit={this.handleSubmit}> <input onChange={this.onChange} value={this.state.text} /> <button>{'Add #' + (this.state.items.length + 1)}</button> </form> </div> ); }});

Page 15: ReactJS / FLUX - Responsive web with DDD and event-driven design

Questions?Re: ReactJS and UI composition

Page 16: ReactJS / FLUX - Responsive web with DDD and event-driven design

Pure ReactJS example// show abdullin.com - static site generated with ReactJS

Page 17: ReactJS / FLUX - Responsive web with DDD and event-driven design

Facebook FLUXEvent-driven pattern for client-side web applications

Page 18: ReactJS / FLUX - Responsive web with DDD and event-driven design
Page 19: ReactJS / FLUX - Responsive web with DDD and event-driven design

Facebook FLUX

• Similar to CQRS

• Compliments ReactJS

• Unidirectional data flow (data flows one-way, unlike MVC but like CQRS)

• This presentation has 3 diffs from “official” Flux

Page 20: ReactJS / FLUX - Responsive web with DDD and event-driven design
Page 21: ReactJS / FLUX - Responsive web with DDD and event-driven design

Essentials

• React components bind to stores, render UI and invoke Event Creators.

• Event Creators are thin (mostly talk to the API).

• All events go through the dispatcher.

• Stores subscribe to events and hold behaviors.

Page 22: ReactJS / FLUX - Responsive web with DDD and event-driven design

Tackling Complexity• Keep ReactJS components thin and focused.

• All behaviors are event-driven.

• All logic is in the stores (we know how to design and test them).

• The only way to change something in the stores (and in the UI) is by publishing an event (no setters)

• console.log

Page 23: ReactJS / FLUX - Responsive web with DDD and event-driven design

Questions?

Page 24: ReactJS / FLUX - Responsive web with DDD and event-driven design

Sample Showtime// Show GTD App

Page 25: ReactJS / FLUX - Responsive web with DDD and event-driven design

Diffs from FLUX

• “Action”(original) -> “Event”

• “Action Creator” (original) -> “Event Creator”

• Declarative Store Subscriptions (Yahoo Fluxible)

Page 26: ReactJS / FLUX - Responsive web with DDD and event-driven design

Isomorphic WebDon’t do it, unless justified

Page 27: ReactJS / FLUX - Responsive web with DDD and event-driven design

Isomorphic Web Apps• Can render on client and server.

• Very fast first page load (time-to-tweet).

• SEO-friendly.

• Server computes state and sends to client with pre-generated HTML.

• Becomes client-side app after page was loaded.

Page 28: ReactJS / FLUX - Responsive web with DDD and event-driven design

Example: /agenda1. Invoke event creators associated with route (all API calls

happen server-side).

2. Dispatch events to all subscribed stores in memory.

3. Render route page component in memory.

4. Send HTML to client, serialize and send store state in 1 batch.

5. Client displays HTML.

6. Client loads client-side scripts, wires components and dehydrates the state.

Page 29: ReactJS / FLUX - Responsive web with DDD and event-driven design

Sample Showtime// Show GTD App Sources

Page 30: ReactJS / FLUX - Responsive web with DDD and event-driven design

Questions?@abdullin / http://abdullin.com

Page 31: ReactJS / FLUX - Responsive web with DDD and event-driven design

Rinat AbdullinRinat Abdullin is a software engineer who spent 10 years in .NET before deflecting to Linux. Along the way he learned from awesome people, contributed to the IDDD book, designed architecture of a social website and helped to deliver big data analytics for retail in the cloud.

Rinat helps teams to scale applications and deal with complex legacy software. He shares his experience in the blog at abdullin.com