React basic by Yoav Amit, Wix
- 2. Who am I?
• Background
• Onavo
• Battlehacks
• OpenRest
• Wix Restaurants
yoava@wix.com https://github.com/3LOK
- 3. React – General Principle
• You write components
• Components have a state and props
• Each components has a render function
Render
Props
State
Virtual
DOM
- 4. React – Get Started
<!DOCTYPE html><html><head>
…
<script src="build/react.js"></script>
<script src="build/react-dom.js"></script>
<script src="https://npmcdn.com/babel-
core@5.8.38/browser.min.js"></script>
…
</head>
<body style="direction:rtl" >
<div id='main'></div>
<script type="text/babel" src="index.jsx"></script>
</body>
</html>
- 6. • Separation of Logic (Flux, Reflux, Redux) and
Views (React)
• Store(s) - Hold the state.
• React – Renders according to state.
• Actions / Reducers – Update the store according
to events
Flux - ?
- 7. Flux - ?
Stores
(hold app state)
React Components
(transform app state to
virtual dom)
Actions update state
- 8. Redux
• Components are divided to two
– Dumb: No state, no actions, all props
– Smart: State from store, wrap dumb components, calls
actions
• Store
– Single Store. Stores all application state
– Triggers event
- 11. Redux-Saga (Advanced)
• Generators
• Write your code in stories (sagas)
– Change the state to show a welcome screen
– Wait for the user to press continue
– Change the state to show the checkout process
- 14. React Native
render: function() {
var movie = …;
return (
<View style={styles.container}>
<Image source={{uri: movie.posters.thumbnail}} style={styles.thumbnail} />
<View style={styles.rightContainer}>
<Text style={styles.title}>{movie.title}</Text>
<Text style={styles.year}>{movie.year}</Text>
</View>
</View>
);
}
- 15. React Native
• Write in Javascript (ES6+) – Use all libraries you’re used to.
• Code is bundled using WebPack into a single JS file.
• React Native Infrastructure rubs code using JavaScriptCore
• “Catches” UI elements generations and executes using OS
API
“Fast-forward a couple of months, and I’m confident enough to say I may never write an iOS
app in Objective-C or Swift again” (link)
- 16. React – Two Common Pitfalls
• Changing state and props directly (react and redux)
• Not using ‘keys’
return (
<div>
{
restaurants.map((rest, index) =>
<Restaurant
title={rest.title}
description={rest.description}
key={rest.id}
/>
)}
</div>
);