39
React Native for Web Developers JS MVC Meetup #15 - March 16, 2016 - Rob Gietema @robgietema

React Native: JS MVC Meetup #15

Embed Size (px)

Citation preview

Page 1: React Native: JS MVC Meetup #15

React Nativefor Web Developers

JS MVC Meetup #15 - March 16, 2016 - Rob Gietema @robgietema

Page 2: React Native: JS MVC Meetup #15

Who am I?

Page 3: React Native: JS MVC Meetup #15

Why React Native?

Page 4: React Native: JS MVC Meetup #15

Native development

Page 5: React Native: JS MVC Meetup #15

UIWebView

Page 6: React Native: JS MVC Meetup #15

React

Page 7: React Native: JS MVC Meetup #15

Hello World Exampleimport React from 'react';

class HelloWorld extends React.Component { render() { return ( <div> <h1>Hello World!</h1> </div> ); } }

export default HelloWorld;

Page 8: React Native: JS MVC Meetup #15

Hello World Exampleimport React from 'react'; import HelloWorld from './hello-world';

React.render( <HelloWorld />, document.body );

Page 9: React Native: JS MVC Meetup #15

Propsimport React from 'react';

class HelloWorld extends React.Component { render() { return ( <div> <h1>Hello from {this.props.author}</h1> </div> ); } }

React.render( <HelloWorld author="John Doe" />, document.body );

Page 10: React Native: JS MVC Meetup #15

Stateclass HelloWorld extends React.Component { constructor(props) { super(props); this.state = { counter: 0 }; }

increase() { this.setState('counter', this.state.counter + 1); }

... }

Page 11: React Native: JS MVC Meetup #15

Nestingimport React from 'react'; import NavBar from './navbar'; import Footer from './footer';

class HelloWorld extends React.Component { render() { return ( <div> <NavBar title="Example App" /> <h1>Hello World!</h1> <Footer /> </div> ); } }

Page 12: React Native: JS MVC Meetup #15

Event Handlersclass HelloWorld extends React.Component { handleClick(event) { alert(this.refs.myInput); }

render() { return ( <div> <h1>Hello from {this.props.author}</h1> <input type="text" ref="myInput" /> <button onClick={this.handleClick} /> </div> ); } }

Page 13: React Native: JS MVC Meetup #15

Lifecycle Methodsclass HelloWorld extends React.Component { componentWillMount() { // Fetch some data }

render() { return ( <h1>Hello from {this.props.author}</h1> ); } }

Page 14: React Native: JS MVC Meetup #15

ReactState > Components > DOM

Page 15: React Native: JS MVC Meetup #15

React NativeState > Components > DOM, iOS Views, Android Views

Page 16: React Native: JS MVC Meetup #15

Learn once write anywhere

Page 17: React Native: JS MVC Meetup #15

What does React Native provide?Touch HandlingNative ComponentsStyle & Layout

Page 18: React Native: JS MVC Meetup #15

How to installMac OS XHomebrewNode >= 4.0

Page 19: React Native: JS MVC Meetup #15

How to installnpm install -g react-native-cli

Page 20: React Native: JS MVC Meetup #15

Optionalnpm install watchmen npm install flow

Page 21: React Native: JS MVC Meetup #15

iOSInstall Xcode

Page 22: React Native: JS MVC Meetup #15

AndroidInstall Android Studio

Page 23: React Native: JS MVC Meetup #15

Getting startedreact-native init ExampleApp

Page 24: React Native: JS MVC Meetup #15

Run Androidreact-native run-android

Page 25: React Native: JS MVC Meetup #15

Run iOS

Page 26: React Native: JS MVC Meetup #15

ComponentsViewTextListViewScrollViewTextInputNavigatorImage...

Page 27: React Native: JS MVC Meetup #15

Components

Page 28: React Native: JS MVC Meetup #15

Native modules

Page 29: React Native: JS MVC Meetup #15

Frameworks

Page 30: React Native: JS MVC Meetup #15

TextInput<TextInput ref="title" autoFocus={true} placeholder={Untitled} style={styles.title} />

<TextInput ref="description" multiline={true} placeholder={Description} style={styles.description} />

Page 31: React Native: JS MVC Meetup #15

Button<TouchableOpacity onPress={() => console.log('pressed')}> <View> <Text>Button</Text> </View> </TouchableOpacity>

Page 32: React Native: JS MVC Meetup #15

Styles and Layoutconst styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', }, instructions: { textAlign: 'center', marginBottom: 5, }, });

Page 33: React Native: JS MVC Meetup #15

Navigator<Navigator initialRoute={{name: 'My First Scene', index: 0}} renderScene={(route, navigator) => // Return view based on route } />

Page 34: React Native: JS MVC Meetup #15

Navigator MethodsgetCurrentRoutes() - returns the current list of routes jumpBack() - Jump backward without unmounting the current scene jumpForward() - Jump forward to the next scene in the route stack jumpTo(route) - Transition to an existing scene without unmounting push(route) - Navigate forward to a new scene pop() - Transition back and unmount the current scene replace(route) - Replace the current scene with a new route replaceAtIndex(route, index) - Replace scene specified by index replacePrevious(route) - Replace the previous scene resetTo(route) - Navigate to a new scene and reset route stack immediatelyResetRouteStack(routeStack) - Reset scene with array popToRoute(route) - Pop to a particular scene popToTop() - Pop to the first scene in the stack

Page 35: React Native: JS MVC Meetup #15

Fetch datafetch(encodeURI('http://myendpoint')) .then(response => { // Handle data });

Page 36: React Native: JS MVC Meetup #15

AsyncStorageasync setMyValue(value) { try { await AsyncStorage.setItem(MY_KEY, value); } catch (error) { // Handle error } }

Page 37: React Native: JS MVC Meetup #15

AsyncStorageasync loadMyValue() { try { let notes = await AsyncStorage.getItem(MY_KEY); } catch (error) { // Handle error } }

Page 38: React Native: JS MVC Meetup #15

AsyncStorageasync removeValue() { try { await AsyncStorage.removeItem(MY_KEY); } catch (error) { // Handle error } }

Page 39: React Native: JS MVC Meetup #15

Questions?slideshare.net/robgietema