RN Fundamentals
RN Fundamentals
<MyComponent />
ReactJS fundamentals
props
<App />
ReactJS fundamentals
React class (stateful) vs function
(stateless)
stateless - function
<App />
ReactJS fundamentals
React class (stateful) vs function
(stateless)
stateless - es5 function declaration
stateful vs stateless
stateless - function declaration hoisting
function App() {
return (
<Text>Hello World</Text>
);
}
ReactJS fundamentals
stateful vs stateless
stateless - es2015 arrow function with explicit return
Mounting
constructor() or property initializers
componentWillMount()
render()
componentDidMount()
read more about property initializers react documentation about property initializers
ReactJS fundamentals
mounting occurs and lifecycle
methods are triggered whenever import React from 'react';
import { View, Text } from 'react-native';
we create a React or React
class App extends React.Component {
Native class constructor() {}
componentWillMount() {}
componentDidMount() {}
render() {
return(
<View>
<Text>{this.state.name}</Text>
</View>
)
}
}
ReactJS fundamentals
lifecycle methods
Component destruction
Unmounting
componentWillUnmount()
ReactJS fundamentals
lifecycle methods
Updating component state and props
shouldComponentUpdate() componentWillReceiveProps()
componentWillUpdate() shouldComponentUpdate()
render() componentWillUpdate()
componentDidUpdate() render()
componentDidUpdate()
ReactJS fundamentals
Creating state
stateful - state using property initializers to declare state
render() {
return(
<Text>{this.state.name}</Text>
)
}
}
ReactJS fundamentals
Creating state
stateful - using constructor to declare state
class Person extends React.Component {
constructor() {
super();
this.state = {
name: 'Chris',
};
}
render() {
return (
<View>
<Text>{this.state.name}</Text>
</View>
);
}
}
ReactJS fundamentals
composing reusable
components - class
stateful vs stateless
props - receiving props in a stateless
import React from 'react';
function import { View, Text } from 'react-native';
stateful vs stateless
import React from 'react';
props - destructuring in stateless import { View, Text } from 'react-native';
component
class App extends Component {
render() {
return (
<Person age={33} name="Chris" />
)
}
};
stateful vs stateless
props - receiving child components as
props const RedContainer = (props) => (
<View style={{ backgroundColor: 'red' }}>
{ props.children }
</View>
);
stateful vs stateless
Default props - class
class Person extends React.Component {
static defaultProps = {
name: 'Chris',
}
render () {
return (
<View>
<Text>{this.props.name}</Text>
</View>
)
}
};
stateful vs stateless
Default props - stateless component
Person.defaultProps = {
name: 'Mike'
};
stateful vs stateless
propTypes
render() {
return (
<View>
<Text>{this.props.name}</Text>
</View>
);
}
};
Person.propTypes = {
name: PropTypes.string.isRequired,
};
updating state
setState import React from 'react';
import { View, Text } from 'react-native';
setState enqueues changes to
the component state and tells class App extends React.Component {
React that this component and state = { name: 'Chris' }
its children need to be re-
rendered with the updated state updateName = () => {
this.setState({ name: 'Amanda' })
}
render() {
return(
<View>
<Text onPress={this.updateName}>{this.state.name}</Text>
</View>
)
}
}
ReactJS fundamentals
updating state
setState
setState enqueues changes to
the component state and tells
SetState
React that this component and shouldComponentUpdate()
its children need to be re-
rendered with the updated state componentWillUpdate()
render()
componentDidUpdate()
ReactJS fundamentals
import React from 'react';
setState import { View, Text } from 'react-native';
render() {
return (
<Person name={this.state.name} onPress={this.updateName} />
)
}
}
updateName = () => {
this.setState({ name: 'Amanda' }, () => {
console.log('name is set!');
})
}
render() {
return (
<Text onPress={this.updateName}>{this.state.name}</Text>
)
}
}
ReactJS fundamentals
setState
State Updates May Be class App extends React.Component {
Asynchronous state = { value: 0 }
login = () => {
this.setState({ loggedIn: true })
}
render() {
const { loggedIn } = this.state;
if (loggedIn) {
return <Text>Logged In</Text>
}
login = () => {
this.setState({ loggedIn: true })
}
render() {
const { loggedIn } = this.state;
return (
<View>
{ loggedIn && <Text>Logged In</Text> }
{ !loggedIn && <Text onPress={this.login}>Please Log In</Text> }
</View>
);
}
ReactJS fundamentals
import {
conditional rendering Platform,
TouchableHighlight,
creating components TouchableNativeFeedback,
View,
on the fly. Text
} from 'react-native';
render() {
let Button = TouchableHighlight;
return (
<View>
<Button onPress={console.log}>
<Text>Hello!</Text>
</Button>
</View>
);
}
ReactJS fundamentals
conditional rendering
filtering data in render.
state = {
sport: 'basketball'
}
render() {
let data = [{ name: 'chris', sport: 'baseball' }, { name: 'James', sport: 'basketball' }]
return (
<View>
{
data.map(d => <Text>{d.name}</Text>)
}
</View>
);
}
ReactJS fundamentals
render() {
const greeting = `${this.state.sport} is my favorite sport`;
return (
<Text>{greeting}</Text>
);
}
}
ReactJS fundamentals
creating variables in render -
stateless component
return (
<Text>{greeting}</Text>
);
};
ReactJS fundamentals
updating with forceUpdate()
updateState = () => {
this.forceUpdate();
}
updateLoggedIn = () => {
this.state.loggedIn = true;
}
render() {
return (
<View>
<Text onPress={this.updateLoggedIn}>Log In</Text>
<Text onPress={this.updateState}>Force Update</Text>
{ this.state.loggedIn && <Text>Logged In</Text>}
</View>
);
}
ReactJS fundamentals
lifecycle -
componentWillMount
componenetWillMount is invoked
immediately before mounting componentWillMount() {
occurs. It is called before this.setState({
render(), therefore setting state startDateTime: new Date(Date.now())
synchronously in this method will });
}
not trigger a re-rendering.
render() {
Documentation recommends return (
using either constructor or <Text>{this.state.startDateTime.toLocaleString()}</Text>
property initializers instead. );
}
There is even a discussion about to
whether this method should be
deprecated.
ReactJS fundamentals
lifecycle -
componentDidMount
componenetDidMount is called
immediately after the render()
method has taken place.
componentDidMount() {
This is a great place to perform fetchFromApi()
Ajax calls, timeouts, or any other .then(data => this.setState({ data }))
}
operations.
This lifecycle method is used a lot in real
world applications.
ReactJS fundamentals
lifecycle -
componentDidMount
componenetDidMount is called
immediately after the render() componentDidMount() {
method has taken place. this.interval = setInterval(() => {
this.setState({
This is a great place to perform tick: this.state.tick + 1,
Ajax calls, timeouts, or any other });
operations. }, 1000);
}
This lifecycle method is used a lot in real
world applications. render() {
return (
<Text>{this.state.tick}</Text>
);
}
ReactJS fundamentals
lifecycle - componentWillUnmount
componenetWillUnmount is
componentDidMount() {
invoked immediately before a this.interval = setInterval(() => {
component is unmounted and this.setState({
destroyed. Perform any tick: this.state.tick + 1,
necessary cleanup in this });
}, 1000);
method, such as invalidating }
timers, canceling network
requests. componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return (
<Text>{this.state.tick}</Text>
);
}
ReactJS fundamentals
lifecycle methods - called in
this order when setState is
called
state changes
shouldComponentUpdate()
componentWillUpdate()
render()
componentDidUpdate()
ReactJS fundamentals
prop changes
componentWillReceiveProps()
shouldComponentUpdate()
componentWillUpdate()
render()
componentDidUpdate()
ReactJS fundamentals
lifecycle - state = {
shouldComponentUpdate tick: 0,
}
returns boolean
shouldComponentUpdate is shouldComponentUpdate(nextProps, nextState) {
if (this.state.tick !== nextState.tick) {
always called before render and return true;
enables the component to figure }
out if a return false;
re-rendering is needed or can be }
skipped. tick = () => {
this.setState({
tick: this.state.tick + 1,
});
}
render() {
return (
<Text onPress={this.tick}>{this.state.tick}</Text>
);
}
ReactJS fundamentals
lifecycle -
componentWillReceiveProps
componentDidMount() {
componentWillReceiveProps is only this.fetchFromApi(this.props.id);
called when the props have changed }
and when this is not an initial rendering.
This method enables us to update the componentWillReceiveProps(nextProps) {
if (this.props.id !== nextProps.id) {
state depending on the existing and
this.fetchFromApi(nextProps.id);
upcoming props. }
}
fetchFromApi(id) {
fetch(`https://someurl/${id}`)
}