SlideShare a Scribd company logo
1 of 13
Collaborne
Using Redux with Polymer
Ronny Roeller (CTO Collaborne)
Collaborne
 Data within app needs to be in sync (focus of
presentation)
 Data between users needs to be in sync
What’s the problem?
Collaborne
 Good: Frontend is heavily decentralized via its
components (Polymer)
 Data binding for centralization
 Problem: every component can mutate the state (+
asynchronicity)
 Very common issue: Facebook private messages
counter led to Flux pattern (not a technology) – see talk
Learn more: “Polymer loves Redux” (Medium)
Components vs. Centralization
Collaborne
 Most widely used Flux implementation
 Three principles
1. Single source of truth (app state is stored in one
object)
2. State is read only
3. Changes are made with pure functions
Learn more: Dan’s introduction and advanced course
What’s Redux?
Collaborne
 UI dispatches actions:
{
type: 'SET_TODO_STATE',
done: true
}
 Reducers listens to actions and create new state:
reducer(state, action) {
if (action.type === 'SET_TODO_STATE') {
return {
status: action.done ? 'done' : 'open'
};
...
Actions & Reducers
Collaborne
How does this help?
* Source: Facebook
Collaborne
 Dev Tools extension (Chrome)
 Time travel / undo
 Actions
 State object
Demo
Collaborne
 Polymer-redux is a super thin Polymer binding library
 Bind a property:
const ReduxBehavior = PolymerRedux(MyReduxStore);
Polymer({
is: "my-element",
properties: {
todos: {
type: Array,
statePath: "todos.all",
readOnly: true
},
},
behaviors: [
ReduxBehavior
]
Connecting Polymer + Redux 1/2
Collaborne
 Dispatch an action:
this.dispatch({
type: 'TOGGLE_TODO',
id: id
});
Learn more: “Polymer loves Redux” (Medium)
Connecting Polymer + Redux 2/2
Collaborne
 UI components shouldn’t know about state shape
 Access derived data (look up table + current person ID)
without unnecessary computation
 Selectors calculate & cache:
const personSelector = Reselect.createSelector(
state => state.ui.person.personId,
state => state.data.people.byId
(personId, byId) => byId[personId]
);
Learn more: “Selectors in polymer-redux” (Medium)
Selectors
Collaborne
 Reducers should be composed (pure functions):
reducer(state, action) {
return {
users: uiUsersReducer(state.users, action),
groups: uiGroupsReducer(state.groups, action)
}
};
• Split UI components from Redux
• Split Redux code by data/ui
• Match component structure with store shape
Learn more: “Organize Large Redux Projects” (Medium)
Code organization
Collaborne
 Generally Polymer and Redux work very well together
 Be careful where Polymer expects mutations (instead of
a new object), e.g. lists might flicker
 Translate between Redux object and Polymer property
var splices = Polymer.ArraySplice.calculateSplices(
reduxItems,
polymerItems);
polymerItems.length = 0;
polymerItems.push(...reduxItems);
this.notifySplices(‘polymerItems’, splices);
Learn more: “Stop Polymer lists from flickering” (Medium)
Polymer vs. Redux clashes
Collaborne
Questions?
@ronnyroeller
Learn more on our Engineering Blog

More Related Content

Similar to Using Redux with Polymer

Similar to Using Redux with Polymer (20)

React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux js
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS Introduction
 
Redux
ReduxRedux
Redux
 
ReactJS (1)
ReactJS (1)ReactJS (1)
ReactJS (1)
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & Redux
 
React & Flux Workshop
React & Flux WorkshopReact & Flux Workshop
React & Flux Workshop
 
Content-Driven Apps with React
Content-Driven Apps with ReactContent-Driven Apps with React
Content-Driven Apps with React
 
A full introductory guide to React
A full introductory guide to ReactA full introductory guide to React
A full introductory guide to React
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
 
React js - The Core Concepts
React js - The Core ConceptsReact js - The Core Concepts
React js - The Core Concepts
 
M03_1_Structur alDiagrams.ppt
M03_1_Structur                         alDiagrams.pptM03_1_Structur                         alDiagrams.ppt
M03_1_Structur alDiagrams.ppt
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to Redux
 
How to use redux with react hooks in react native application
How to use redux with react hooks in react native applicationHow to use redux with react hooks in react native application
How to use redux with react hooks in react native application
 
Redux js
Redux jsRedux js
Redux js
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
[@NaukriEngineering] Flux Architecture
[@NaukriEngineering] Flux Architecture[@NaukriEngineering] Flux Architecture
[@NaukriEngineering] Flux Architecture
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
React - An Introduction
React - An IntroductionReact - An Introduction
React - An Introduction
 
Reducers+flux=redux
Reducers+flux=reduxReducers+flux=redux
Reducers+flux=redux
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 

Using Redux with Polymer

  • 1. Collaborne Using Redux with Polymer Ronny Roeller (CTO Collaborne)
  • 2. Collaborne  Data within app needs to be in sync (focus of presentation)  Data between users needs to be in sync What’s the problem?
  • 3. Collaborne  Good: Frontend is heavily decentralized via its components (Polymer)  Data binding for centralization  Problem: every component can mutate the state (+ asynchronicity)  Very common issue: Facebook private messages counter led to Flux pattern (not a technology) – see talk Learn more: “Polymer loves Redux” (Medium) Components vs. Centralization
  • 4. Collaborne  Most widely used Flux implementation  Three principles 1. Single source of truth (app state is stored in one object) 2. State is read only 3. Changes are made with pure functions Learn more: Dan’s introduction and advanced course What’s Redux?
  • 5. Collaborne  UI dispatches actions: { type: 'SET_TODO_STATE', done: true }  Reducers listens to actions and create new state: reducer(state, action) { if (action.type === 'SET_TODO_STATE') { return { status: action.done ? 'done' : 'open' }; ... Actions & Reducers
  • 6. Collaborne How does this help? * Source: Facebook
  • 7. Collaborne  Dev Tools extension (Chrome)  Time travel / undo  Actions  State object Demo
  • 8. Collaborne  Polymer-redux is a super thin Polymer binding library  Bind a property: const ReduxBehavior = PolymerRedux(MyReduxStore); Polymer({ is: "my-element", properties: { todos: { type: Array, statePath: "todos.all", readOnly: true }, }, behaviors: [ ReduxBehavior ] Connecting Polymer + Redux 1/2
  • 9. Collaborne  Dispatch an action: this.dispatch({ type: 'TOGGLE_TODO', id: id }); Learn more: “Polymer loves Redux” (Medium) Connecting Polymer + Redux 2/2
  • 10. Collaborne  UI components shouldn’t know about state shape  Access derived data (look up table + current person ID) without unnecessary computation  Selectors calculate & cache: const personSelector = Reselect.createSelector( state => state.ui.person.personId, state => state.data.people.byId (personId, byId) => byId[personId] ); Learn more: “Selectors in polymer-redux” (Medium) Selectors
  • 11. Collaborne  Reducers should be composed (pure functions): reducer(state, action) { return { users: uiUsersReducer(state.users, action), groups: uiGroupsReducer(state.groups, action) } }; • Split UI components from Redux • Split Redux code by data/ui • Match component structure with store shape Learn more: “Organize Large Redux Projects” (Medium) Code organization
  • 12. Collaborne  Generally Polymer and Redux work very well together  Be careful where Polymer expects mutations (instead of a new object), e.g. lists might flicker  Translate between Redux object and Polymer property var splices = Polymer.ArraySplice.calculateSplices( reduxItems, polymerItems); polymerItems.length = 0; polymerItems.push(...reduxItems); this.notifySplices(‘polymerItems’, splices); Learn more: “Stop Polymer lists from flickering” (Medium) Polymer vs. Redux clashes