Last active
February 27, 2018 09:16
-
-
Save kashishgrover/179a8cd0c6ee0e6dd211b0b39786ac80 to your computer and use it in GitHub Desktop.
Writing Stateless Components in React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Consider a situation where you pass data, a state, and your navigation as props to your child component. | |
const MyComponent = ({ loadingImages, data, navigation }) => { | |
const loadNextPage = () => { | |
navigation.navigate('NextPage'); | |
} | |
return ( | |
<View> | |
{ | |
loadingImages ? | |
<ActivityIndicator /> | |
: | |
<Image source={{ uri: data.image }} /> | |
} | |
<View> | |
<Text>{data.title}</Text> | |
<Text>{data.subtitle}</Text> | |
</View> | |
<TouchableOpacity | |
onPress={loadNextPage} | |
activeOpacity={0.8} | |
> | |
<Text>Click here for next page.</Text> | |
</TouchableOpacity> | |
</View> | |
); | |
}; | |
export default MyComponent; | |
/* | |
USAGE in a Screen -- ParentScreen.js | |
Consider you called your API in the screen which loads a bunch of images. This data was then inserted into this.data object. | |
When the API gets called and the parent rerenders, so does the child. | |
<MyComponent | |
loadingImages={this.state.loadingImages} | |
data={this.data} | |
navigation={this.props.navigation} | |
/> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment