Alfian Busyro
JavaScript and Web Performance Addict
Ex Nano Team
(June 2017-October 2017)
Thread
Rendering update
Rendering update
Technologies behind the layer
Yoga
Internal
MessageQueue
import MessageQueue from 'react-native/Libraries/Utilities/MessageQueue'
MessageQueue.spy((info)=>console.log("event!", info))
React Lifecycle
shouldComponentUpdate(nextProps, nextState) {
return (
(!isEqual(this.props.a, nextProps.a)) ||
(!isEqual(this.state.data, nextState.data))
)
}
// From
export default class AwesomeComponent extends React.Component {
// To
export default class AwesomeComponent extends React.PureComponent {
class MyComponent extends React.PureComponent {
render() {
return this.props.data.map((item, i) => {
return <Text key={item.id}>{item.title}</Text>
});
}
}
export default class AwesomeComponent extends Component {
glamorousAsyncProcess() {
// redux action that remains forever
}
constructor() {
super()
// don't
glamorousAsyncProcess()
}
componentWillMount() {
// don't
glamorousAsyncProcess()
}
async populateUser() {
// populate 1M Users
await this.props.populateMillionUsers()
}
render() {
//don't
populateUser()
return(
<View>
<Text>
Hello World! with heavy process on the back
</Text>
</View>
)
}
}
class AwesomeComponent extends Component {
glamorousAsyncProcess() {
// redux action that remains forever
}
constructor() {
super()
this.viewContacts = this.viewContacts.bind(this)
}
componentDidMount() {
// do
glamorousAsyncProcess()
}
viewContacts() {
// Show Contact List
}
render() {
return(
<View>
<Text>
Hello World! with heavy process on the back
</Text>
<TouchableOpacity onPress={this.viewContacts} >
<Text>Press Me!</Text>
</TouchableOpacity>
</View>
)
}
}
Advantages:
Common issue:
import nextFrame from 'next-frame';
// ...
for (let recording of recordingsJSON) {
await nextFrame(); // This is all we need to add!
mergeRecordingToLocalDatabase(recording);
}
https://github.com/corbt/next-frame
https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
Double defenses
let setDefaultAddressInRedux = async () => {
await nextFrame()
setTimeout(() => {
self.props.setDefaultAddressInRedux(selectedDefaultAddress)
}, 0)
}
setDefaultAddressInRedux()
// Avoid these
let hitam = 'hitam pekat'
// di lain tempat
let hitam = 'hitam cerah'
let tambah = (a, b) => a +b
// di lain tempat
let tambah = (a) => a + 1
You Might Not Need Redux
Redux hanya diperlukan untuk state yang dibutuhkan oleh beberapa Component yang berbeda
- Alfian
Context API
import React, {Component } from 'react'
import createReactContext from 'create-react-context'
import { getPhotos } from '../Services/Apis/Search'
const SearcContext = createReactContext({
data: null
})
export const SearchConsumer = SearcContext.Consumer
export class SearchProvider extends Component {
state = {
data: null,
isFetching: false,
error: false
}
request = async (tag) => {
this.setState({
isFetching: true
})
let data = await getPhotos(tag)
let arrayPost = data.posts.map((obj, i) => {
obj.key = i
return obj
})
this.setState({
data: arrayPost,
error: false,
isFetching: false
})
}
render() {
const { data, isFetching, error } = this.state
return(
<SearcContext.Provider
value={{
data: data,
isFetching: isFetching,
error: error,
request: this.request
}}
>
{this.props.children}
</SearcContext.Provider>
)
}
}
is a simple, unencrypted, asynchronous, persistent, key-value storage system that is global to the app
accessible={true}
accessibilityLabel="You can hear me via VoiceOver"
AccessibilityTraits / AccessibilityRole
move files around until it feels right
Inquiries :
By Alfian Busyro