On Github bsonntag / react-talk
A talk about the React world.
Created by Benjamim Sonntag for a siosLIFE talk & roll.
"A JavaScript library for building user interfaces."
Made by Facebook and Instagram
Makes no assumptions about the rest of the application.
It's the V in MVC.
No overload of features to explore (and get lost in).
Fast to start using.
Used for efficient re-rendering of the DOM.
The DOM is only updated when the virtual DOM changes, and only the parts that change are updated.
React is all about building components.
Components are not templates.
Components unify markup with its view logic.
(Yes, JavaScript and HTML in the same file. Yuck...)
Components should be encapsulated and reusable.
Makes them easier to maintain.
A component's properties are changed by its parent.
The state is updated by the component itself or by external events.
When the state is updated, the view will re-render itself.
Components can be seen as state machines.
Feels like game programming!
while(true) { processInput(); update(); render(); }
(From gameprogrammingpatterns.com)
var HelloMessage = React.createClass({ render: function() { return <div> Hello {this.props.name}! </div>; } });
var ClickCounter = React.createClass({ getInitialState: function() { return { count: 0 }; }, increment: function() { this.setState({ count: this.state.count + 1 }); }, render: function() { return <div> Clicked {this.state.count} times. <button onClick={this.increment}></button> </div>; } });
var App = React.createClass({ render: function() { return <div> <HelloMessage name="Maria Rosa"></HelloMessage> <ClickCounter></ClickCounter> </div>; } });
The object passed to React.createClass() specifies the component's methods, including lifecycle methods.
Must return a single DOM element, that can be another React component or a native DOM element (like a <div>)
The render method is required.
Values in the returned object will be set on this.props if that prop is not specified by the parent component.
Invoked once and cached when the class is created.
The return value will be used as the initial value of this.state.
Invoked before a component is mounted.
Merges nextState into the current state.
Clears the component's state and sets it to nextState.
void componentWillMount() void componentDidMount() void componentWillReceiveProps(object nextProps) boolean shouldComponentUpdate(object nextProps, object nextState) void componentWillUpdate(object nextProps, object nextState) void componentDidUpdate(object prevProps, object prevState) void componentWillUnmount()
"Application Architecture for Building User Interfaces."
Complements React's composable view components by utilizing a unidirectional data flow.
It's more of a pattern rather than a formal framework.The dispatcher is the central hub that manages all data flow.
Receives actions from views and external sources and sends them to the stores.
Contain the application state and logic.
Actions result in updates to the state of the store.
After the stores are updated, they broadcast an event declaring that their state has changed.
Views listen for events that are broadcasted by the stores.
When an event is received, the view requests the new state via the stores's public getters.
"Predictable state container for JavaScript applications."
"A framework for building native apps using React."
var App = React.createClass({ render: function() { return <DrawerLayoutAndroid renderNavigationView={this.nav}> <ProgressBarAndroid /> </DrawerLayoutAndroid>; }, nav: function() { return <Text> React Native </Text>; } });
var TouchDemo = React.createClass({ render: function() { return <ScrollView> <TouchableHighlight onPress={this.touched}> <Text>Proper Touch Handling</Text> </TouchableHighlight> </ScrollView>; }, touched: function() { console.log('touched!'); } });
Uses CSS-like syntax and supports flexbox natively.
var styles = StyleSheet.create({ image: { width: 40, height: 40, marginRight: 10 }, title: { fontSize: 11, fontWeight: 'bold' }, text: { flex: 1, justifyContent: 'center' } });
All operations between the JavaScript application code and the native platform are performed asynchronously.
Provides polyfils for most browser APIs, like XMLHttpRequest.
There are also many packages on npm that polyfil other browser APIs, like WebRTC.
React Native is designed to be easily extended with custom native views and modules.
public class MyCustomModule extends ReactContextBaseJavaModule { @ReactMethod public void processString(String input, Callback callback) { callback.invoke(input.replace("Goodbye", "Hello")); } }
Uses a JavaScript runtime, but the UI is not HTML.
Provides a native-level performance and look and feel.