introduction to react native & redux
TRANSCRIPT
![Page 1: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/1.jpg)
INTRODUCTION TO THE REACT STACKBarak Cohen, Wondermall
![Page 2: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/2.jpg)
FRONT-END IS BROKEN• Build the same app 6 times: iOS (Phone/Tablet), Android
(Phone/Tablet), Web (Desktop/Phone)
• Different teams build the same thing using 3 different languages, frameworks & libraries.
• Hard to maintain feature parity & consistent terminology.
• Impossible to be an expert in all technologies and “own” something in all products
![Page 3: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/3.jpg)
IOS IS BROKEN
• iOS development is slow, verbose language & frameworks
• Imperative, statefull UI which is error prone
• No out-of-the-box solutions for state management
• Slow release cycle caused by AppStore reviews
![Page 4: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/4.jpg)
REACT-NATIVE IS…• JavaScript for Application Logic
• Native UI (No WebViews!)
• Flexbox Layout
• Functional UI
• Built on top of ReactJS - widely used by FB
![Page 5: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/5.jpg)
REACT (NATIVE+JS) BENEFITS• “Learn once write anywhere”
• Reuse almost all non-UI code across all platforms
• Reuse most UI code between Native Platforms
• Declarative state management using Redux/Relay
• Hot-reloading JS, debug in Chrome
![Page 6: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/6.jpg)
BENEFITS - CON’T
• Can be added incrementally to an existing app
• Over-the-wire updates w/o AppStore (AppHub)
• JS w/ ES6 is succinct and productive
• Can use previously written native code and UI
![Page 7: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/7.jpg)
HELLO WORLDvar React = require('react-native')var { View, Text } = React
class HelloWorldView extends React.Component { render() { return ( <View><Text>Hello World</Text></View> ) }}
![Page 8: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/8.jpg)
ADDING STYLEclass HelloWorldView extends React.Component { ...
render() { return ( <View style={{padding: 10}}> <Text style={{fontSize: 14, color: '#0000ff'}}> Hello World </Text> </View> ) }}
![Page 9: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/9.jpg)
FLEXBOX LAYOUT
class HelloWorldView extends React.Component { render() { return ( <View style={{flexDirection: 'column', flex: 1}}> <View style={{flex: 1, backgroundColor: 'red'}}></View> <View style={{flex: 1, backgroundColor: 'green'}}></View> <View style={{flex: 1, backgroundColor: 'blue'}}></View> </View> ) }}
![Page 10: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/10.jpg)
FLEXBOX LAYOUT - 2
class HelloWorldView extends React.Component { render() { return ( <View style={{flexDirection: 'row', flex: 1}}> <View style={{flex: 1, backgroundColor: 'red'}}></View> <View style={{flex: 1, backgroundColor: 'green'}}></View> <View style={{flex: 1, backgroundColor: 'blue'}}></View> </View> ) }}
![Page 11: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/11.jpg)
ADDING STATEclass HelloWorldView extends React.Component { constructor(props) { super(props) this.state = {msg: ‘Hello World'} }
render() { return ( <View> <Text>{this.state.msg}</Text> </View> ) }}
![Page 12: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/12.jpg)
MUTATING STATEclass HelloWorldView extends React.Component { onPress() { this.setState(msg: ‘React Rulez') }
render() { return ( <TouchableOpacity onPress={this.onPress}> <Text>{this.state.msg}</Text> </TouchableOpacity> ) }}
![Page 13: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/13.jpg)
INTERFACE (OPTIONAL)
class TabBar extends React.Component { static propTypes = { goToPage: React.PropTypes.func, activeTab: React.PropTypes.number, tabs: React.PropTypes.array.isRequired }; render() { ...{this.props.tabs[this.props.activeTab]}... }}
![Page 14: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/14.jpg)
COMPOSITIONvar FeedsView = require('./FeedsView')...
class TabsView extends React.Component { render() { return ( <FeedsView/> <SearchView/> <CartView/> <AccountView user={this.props.user}/> ) }}
![Page 15: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/15.jpg)
REDUXPredictable State Management
![Page 16: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/16.jpg)
REDUX IS…
• Simplified implementation of the Flux pattern
• App state is stored in a single object tree (“Store”)
• To change state emit an “Action” - an object describing the change
• “Reducers” receive actions and mutate the “Store”
![Page 17: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/17.jpg)
EXAMPLEfunction counter(state = 0, action) { switch (action.type) { case 'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; }}
let store = createStore(counter);store.dispatch({ type: 'INCREMENT' });store.dispatch({ type: 'INCREMENT' });store.dispatch({ type: 'DECREMENT' });
![Page 18: Introduction to React Native & Redux](https://reader031.vdocuments.mx/reader031/viewer/2022020119/5871aba11a28abda6a8b58e5/html5/thumbnails/18.jpg)
BENEFITS
• Centralized repository for state (both data and UI)
• Completely decoupled from the UI w/ separate lifecycle
• Pure business-logic reusable across platforms