SlideShare a Scribd company logo
N Things You Don’t Want to
Repeat in React Native
Antipatterns and pitfalls
What is React Native?
JavaScript, React
Redux / MobX / ...
Java & Objective C
Android & iOS
React Native
A lot of opportunities
for bad code & design
$ npm i -g react-native-cli
$ react-native init TheNextBigThing
$ npm i
$ npm run start
$ react-native run-android
$ react-native run-ios
I am God
It is the future
─ Let’s update React Native!
─ Sorry, I know I’ve asked only for Android version, may we add iOS
support after 3 months of development?
─ We need push notifications and in app purchases
─ YAHOOOOO! Our library has updated!
─ Play around with some animations
─ Windows Phone support…?
─ Actions, reducers, immutability, higher order components, etc
─ Where to store X? AsyncStorage? Redux? Local State?
─ So what’s about Windows Phone?
Hello? Is there anybody?
SH*T!
React
React Component Lifecycle
componentWillMount General Configurations, sockets / services init
componentDidMount Loading data, setState
componentWillReceiveProps Update state based on new props
shouldComponentUpdate Optimization
componentWillUpdate Something before render (no setState)
componentDidUpdate Something after render ¯_(ツ)_/¯
componentWillUnmount Clean, remove listeners
React
1. props in initialState
2. setTimeout, setInterval
3. Not using defaultProps and propTypes
4. Impure render()
5. Logic in render()
6. setState() in componentWillMount()
7. Mutating state
8. Strings refs
9. Usings indexes as keys
10. Not using core components principles (smart, dump, higher-order components)
1. Props in Initial State
class SomeComponent extends Component {
constructor(props) {
super(props);
this.state = {
displayMe: this.props.data
}
}
render() {
return (
<Text>{ this.state.displayMe }</Text>
);
}
}
class SomeComponent extends Component {
render() {
return (
<Text>
{this.props.displayMe}
</Text>
);
}
}
2. setTimeout
setInterval
class LightSwitch extends Component {
state = {
on: false
};
toggle = () => {
this.setState({
on: !this.state.on
})
};
handleClick = () => {
this.props.setTimeout(this.toggle, 5000);
};
render () {
const { on } = this.state;
const bgColor = on ? 'yellow' : 'gray';
return (
<View style={{ backgroundColor: bgColor }}>
<TouchableOpacity onClick={this.handleClick}>
Toggle!
</TouchableOpacity>
</View>
);
}
}
export default ReactTimeout(LightSwitch);
3. Not using Default props & Prop Types
import PropTypes from 'prop-types';
class SampleComponent extends Component {
static propTypes = {
item: PropTypes.object
};
static defaultProps = {
item: { name: '', price: 0 }
};
render() {
return (
<View>
<Text>this.props.item.name</Text>
<Text>this.props.item.price</Text>
</View>
);
}
}
4. Impure render()
5. Logic in render()
6. setState in componentWillMount
class SomeComponent extends Component {
componentWillMount() {
axios.get('/api/users').then(res => {
this.setState({users: res.data.users});
});
}
// ...
}
class SomeComponent extends Component {
componentDidMount() {
axios.get('/api/users').then(res => {
this.setState({users: res.data.users});
});
}
// ...
}
7. Mutating State
class SomeComponent extends Component {
constructor(props) {
super(props);
this.state = { items: ['foo', 'bar'] };
}
handleClick = () => {
this.state.items.push('lorem');
this.setState({ items: this.state.items });
};
render() {
return (
<View>
{this.state.items.length}
<TouchableOpacity onClick={this.handleClick}>
+
</TouchableOpacity>
</View>
)
}
}
class SomeComponent extends Component {
constructor(props) {
super(props);
this.state = { items: ['foo', 'bar'] };
}
handleClick = () => {
this.setState({
items: this.state.items.concat('lorem')
});
};
render() {
return (
<View>
{this.state.items.length}
<TouchableOpacity onClick={this.handleClick}>
+
</TouchableOpacity>
</View>
)
}
}
8. String Refs
class SomeComponent extends Component {
render() {
return (
<View>
<TextInput ref={'txtInput'}/>
</View>
);
}
}
class SomeComponent extends Component {
render() {
return (
<View>
<TextInput ref={txtInput => {
this.txtInput = txtInput }}/>
</View>
);
}
}
9. Using indexes as keys
{todos.map((todo, index) =>
<Todo
{...todo}
key={index}
/>
)}
{todos.map((todo) =>
<Todo
{...todo}
key={todo.id}
/>
)}
10. Not using Dumb / Smart / Higher Order
Components concepts
Redux
Redux
1. Wrong FLUX understanding & wrong store structure
2. Dumb mapStateToProps
3. Form validation with redux store
4. Too much dispatches
5. Business logic in reducers
2. Dump mapStateToProps
class ConfInfo extends Component {
static propTypes = {
conference: PropTypes.shape({
name: PropTypes.string,
type: PropTypes.string,
talks: PropTypes.array
}),
attendees: PropTypes.array
};
render() {
const { conference } = this.props;
const { talks, type } = conference;
// usage
}
}
function mapStateToProps({ conference, attendees }) {
return { conference, attendees };
}
export default connect(mapStateToProps)(ConfInfo);
class ConfInfo extends Component {
static propTypes = {
type: ConferenceTypeShape,
talks: ArrayOfConferenceTalksShapes
};
render() {
const { talks, type } = this.props;
// usage
}
}
function mapStateToProps({ conference }) {
const { talks, type } = conference;
return { talks, type };
}
export default connect(mapStateToProps)(ConfInfo)
3. Form Validation with Redux
4. Too Much Dispatches
5. Business Logic in Reducers
React Native
Where to store X?
AsyncStorage Cache, cross session data
Local State UI state & Forms data
Redux Store App State & Cross component data
this Data, that does not trigger updates
static Utils
React Native
1. Updates just for updates
2. Imprudent packages choosing
3. Being scary about native code
4. Estimating Android & iOS equally
5. Developing Android & iOS in a sequence
6. Not testing on devices
7. Relying on JS single thread (animations, calculations, etc)
8. Using z-index a lot
7. Worker polyfill
// sample.worker.js
import { WorkerService } from 'rn-worker';
const worker = new WorkerService();
worker.onmessage = message => {
worker.postMessage(`Hello from the other side (${message})`)
};
// sample.component.js
import { Worker } from 'rn-worker';
export class Sample extends React.Component {
componentDidMount () {
const PORT = 8083;
this.worker = new Worker(PORT);
this.worker.onmessage = message => this.setState({
text: message
});
this.worker.postMessage("Hey Worker!")
}
componentWillUnmount () {
this.worker.terminate();
}
}
Much
Better
Thank You!
Will be happy to answer your questions now, after
conf and online:
kulyk.anton
kuliks.anton@gmail.com

More Related Content

PPT
Javascript Design Patterns
PDF
Java Script Best Practices
PDF
Headless Js Testing
PDF
Bottom Up
PPTX
Java script
PPT
Wakanday JS201 Best Practices
PPTX
LinkedIn TBC JavaScript 100: Functions
PDF
Asynchronous JS in Odoo
Javascript Design Patterns
Java Script Best Practices
Headless Js Testing
Bottom Up
Java script
Wakanday JS201 Best Practices
LinkedIn TBC JavaScript 100: Functions
Asynchronous JS in Odoo

What's hot (20)

PDF
Advanced javascript
PDF
Anonymous functions in JavaScript
PDF
JavaScript Functions
PPT
Advanced JavaScript
PDF
OSCON - ES6 metaprogramming unleashed
PDF
What's up with Prototype and script.aculo.us?
PDF
Declarative presentations UIKonf
PDF
Douglas Crockford: Serversideness
PDF
Swift で JavaScript 始めませんか? #iOSDC
PDF
Func up your code
PPTX
Workshop 1: Good practices in JavaScript
PDF
Workshop 5: JavaScript testing
PDF
Java script object model
PPTX
Intro to Javascript
PDF
Design patterns in javascript
PDF
Functional Javascript
PPTX
Java script advance-auroskills (2)
PPTX
AngularJS with TypeScript and Windows Azure Mobile Services
PPTX
AngularJs $provide API internals & circular dependency problem.
PPTX
2. Design patterns. part #2
Advanced javascript
Anonymous functions in JavaScript
JavaScript Functions
Advanced JavaScript
OSCON - ES6 metaprogramming unleashed
What's up with Prototype and script.aculo.us?
Declarative presentations UIKonf
Douglas Crockford: Serversideness
Swift で JavaScript 始めませんか? #iOSDC
Func up your code
Workshop 1: Good practices in JavaScript
Workshop 5: JavaScript testing
Java script object model
Intro to Javascript
Design patterns in javascript
Functional Javascript
Java script advance-auroskills (2)
AngularJS with TypeScript and Windows Azure Mobile Services
AngularJs $provide API internals & circular dependency problem.
2. Design patterns. part #2
Ad

Similar to N Things You Don't Want to Repeat in React Native (20)

PDF
Building-with-React-Native-Dont-Make-These-Mistakes.pdf
PDF
React native: building native iOS apps with javascript
PDF
React native
PPTX
JS Fest 2018. Илья Иванов. Введение в React-Native
PPTX
React Native: Introduction
PDF
React && React Native workshop
PDF
Challenges of React Native App Development_ Effective Mitigation Strategies.pdf
PDF
Connect.js - Exploring React.Native
PDF
"React Native" by Vanessa Leo e Roberto Brogi
PDF
Pieter De Baets - An introduction to React Native
PDF
React Native: JS MVC Meetup #15
PDF
Introduction to react native
PDF
How React Native has changed Web and Mobile Application Development, Engineer...
PDF
React Best Practices All Developers Should Follow in 2024.pdf
PDF
An Emoji Introduction to React Native (Panagiotis Vourtsis, Senior Front End ...
PDF
Introduzione a React Native - Facebook Developer Circle Rome
PPTX
React JS - A quick introduction tutorial
PPTX
React native introduction
PDF
React Native Workshop - React Alicante
PDF
Intro to ReactJS
Building-with-React-Native-Dont-Make-These-Mistakes.pdf
React native: building native iOS apps with javascript
React native
JS Fest 2018. Илья Иванов. Введение в React-Native
React Native: Introduction
React && React Native workshop
Challenges of React Native App Development_ Effective Mitigation Strategies.pdf
Connect.js - Exploring React.Native
"React Native" by Vanessa Leo e Roberto Brogi
Pieter De Baets - An introduction to React Native
React Native: JS MVC Meetup #15
Introduction to react native
How React Native has changed Web and Mobile Application Development, Engineer...
React Best Practices All Developers Should Follow in 2024.pdf
An Emoji Introduction to React Native (Panagiotis Vourtsis, Senior Front End ...
Introduzione a React Native - Facebook Developer Circle Rome
React JS - A quick introduction tutorial
React native introduction
React Native Workshop - React Alicante
Intro to ReactJS
Ad

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
KodekX | Application Modernization Development
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
cuic standard and advanced reporting.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Review of recent advances in non-invasive hemoglobin estimation
Unlocking AI with Model Context Protocol (MCP)
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KodekX | Application Modernization Development
Digital-Transformation-Roadmap-for-Companies.pptx
Electronic commerce courselecture one. Pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Per capita expenditure prediction using model stacking based on satellite ima...
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation_ Review paper, used for researhc scholars
MYSQL Presentation for SQL database connectivity
Chapter 3 Spatial Domain Image Processing.pdf
MIND Revenue Release Quarter 2 2025 Press Release
cuic standard and advanced reporting.pdf
Network Security Unit 5.pdf for BCA BBA.
Advanced methodologies resolving dimensionality complications for autism neur...
“AI and Expert System Decision Support & Business Intelligence Systems”
Spectral efficient network and resource selection model in 5G networks
Review of recent advances in non-invasive hemoglobin estimation

N Things You Don't Want to Repeat in React Native

  • 1. N Things You Don’t Want to Repeat in React Native Antipatterns and pitfalls
  • 2. What is React Native?
  • 3. JavaScript, React Redux / MobX / ... Java & Objective C Android & iOS React Native
  • 4. A lot of opportunities for bad code & design
  • 5. $ npm i -g react-native-cli $ react-native init TheNextBigThing $ npm i $ npm run start $ react-native run-android $ react-native run-ios
  • 6. I am God It is the future
  • 7. ─ Let’s update React Native! ─ Sorry, I know I’ve asked only for Android version, may we add iOS support after 3 months of development? ─ We need push notifications and in app purchases ─ YAHOOOOO! Our library has updated! ─ Play around with some animations ─ Windows Phone support…? ─ Actions, reducers, immutability, higher order components, etc ─ Where to store X? AsyncStorage? Redux? Local State? ─ So what’s about Windows Phone? Hello? Is there anybody?
  • 10. React Component Lifecycle componentWillMount General Configurations, sockets / services init componentDidMount Loading data, setState componentWillReceiveProps Update state based on new props shouldComponentUpdate Optimization componentWillUpdate Something before render (no setState) componentDidUpdate Something after render ¯_(ツ)_/¯ componentWillUnmount Clean, remove listeners
  • 11. React 1. props in initialState 2. setTimeout, setInterval 3. Not using defaultProps and propTypes 4. Impure render() 5. Logic in render() 6. setState() in componentWillMount() 7. Mutating state 8. Strings refs 9. Usings indexes as keys 10. Not using core components principles (smart, dump, higher-order components)
  • 12. 1. Props in Initial State class SomeComponent extends Component { constructor(props) { super(props); this.state = { displayMe: this.props.data } } render() { return ( <Text>{ this.state.displayMe }</Text> ); } } class SomeComponent extends Component { render() { return ( <Text> {this.props.displayMe} </Text> ); } }
  • 13. 2. setTimeout setInterval class LightSwitch extends Component { state = { on: false }; toggle = () => { this.setState({ on: !this.state.on }) }; handleClick = () => { this.props.setTimeout(this.toggle, 5000); }; render () { const { on } = this.state; const bgColor = on ? 'yellow' : 'gray'; return ( <View style={{ backgroundColor: bgColor }}> <TouchableOpacity onClick={this.handleClick}> Toggle! </TouchableOpacity> </View> ); } } export default ReactTimeout(LightSwitch);
  • 14. 3. Not using Default props & Prop Types import PropTypes from 'prop-types'; class SampleComponent extends Component { static propTypes = { item: PropTypes.object }; static defaultProps = { item: { name: '', price: 0 } }; render() { return ( <View> <Text>this.props.item.name</Text> <Text>this.props.item.price</Text> </View> ); } }
  • 15. 4. Impure render() 5. Logic in render()
  • 16. 6. setState in componentWillMount class SomeComponent extends Component { componentWillMount() { axios.get('/api/users').then(res => { this.setState({users: res.data.users}); }); } // ... } class SomeComponent extends Component { componentDidMount() { axios.get('/api/users').then(res => { this.setState({users: res.data.users}); }); } // ... }
  • 17. 7. Mutating State class SomeComponent extends Component { constructor(props) { super(props); this.state = { items: ['foo', 'bar'] }; } handleClick = () => { this.state.items.push('lorem'); this.setState({ items: this.state.items }); }; render() { return ( <View> {this.state.items.length} <TouchableOpacity onClick={this.handleClick}> + </TouchableOpacity> </View> ) } } class SomeComponent extends Component { constructor(props) { super(props); this.state = { items: ['foo', 'bar'] }; } handleClick = () => { this.setState({ items: this.state.items.concat('lorem') }); }; render() { return ( <View> {this.state.items.length} <TouchableOpacity onClick={this.handleClick}> + </TouchableOpacity> </View> ) } }
  • 18. 8. String Refs class SomeComponent extends Component { render() { return ( <View> <TextInput ref={'txtInput'}/> </View> ); } } class SomeComponent extends Component { render() { return ( <View> <TextInput ref={txtInput => { this.txtInput = txtInput }}/> </View> ); } }
  • 19. 9. Using indexes as keys {todos.map((todo, index) => <Todo {...todo} key={index} /> )} {todos.map((todo) => <Todo {...todo} key={todo.id} /> )}
  • 20. 10. Not using Dumb / Smart / Higher Order Components concepts
  • 21. Redux
  • 22. Redux 1. Wrong FLUX understanding & wrong store structure 2. Dumb mapStateToProps 3. Form validation with redux store 4. Too much dispatches 5. Business logic in reducers
  • 23. 2. Dump mapStateToProps class ConfInfo extends Component { static propTypes = { conference: PropTypes.shape({ name: PropTypes.string, type: PropTypes.string, talks: PropTypes.array }), attendees: PropTypes.array }; render() { const { conference } = this.props; const { talks, type } = conference; // usage } } function mapStateToProps({ conference, attendees }) { return { conference, attendees }; } export default connect(mapStateToProps)(ConfInfo); class ConfInfo extends Component { static propTypes = { type: ConferenceTypeShape, talks: ArrayOfConferenceTalksShapes }; render() { const { talks, type } = this.props; // usage } } function mapStateToProps({ conference }) { const { talks, type } = conference; return { talks, type }; } export default connect(mapStateToProps)(ConfInfo)
  • 24. 3. Form Validation with Redux 4. Too Much Dispatches 5. Business Logic in Reducers
  • 26. Where to store X? AsyncStorage Cache, cross session data Local State UI state & Forms data Redux Store App State & Cross component data this Data, that does not trigger updates static Utils
  • 27. React Native 1. Updates just for updates 2. Imprudent packages choosing 3. Being scary about native code 4. Estimating Android & iOS equally 5. Developing Android & iOS in a sequence 6. Not testing on devices 7. Relying on JS single thread (animations, calculations, etc) 8. Using z-index a lot
  • 28. 7. Worker polyfill // sample.worker.js import { WorkerService } from 'rn-worker'; const worker = new WorkerService(); worker.onmessage = message => { worker.postMessage(`Hello from the other side (${message})`) }; // sample.component.js import { Worker } from 'rn-worker'; export class Sample extends React.Component { componentDidMount () { const PORT = 8083; this.worker = new Worker(PORT); this.worker.onmessage = message => this.setState({ text: message }); this.worker.postMessage("Hey Worker!") } componentWillUnmount () { this.worker.terminate(); } }
  • 30. Thank You! Will be happy to answer your questions now, after conf and online: kulyk.anton kuliks.anton@gmail.com