DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • Cross-Platform Mobile Application Development: Evaluating Flutter, React Native, HTML5, Xamarin, and Other Frameworks
  • Creating a React Native SDK: Step-By-Step Guide With Practical Examples
  • ReactJS for AI and Machine Learning: A Powerful Combination

Trending

  • How to Submit a Post to DZone
  • Using Python Libraries in Java
  • The Smart Way to Talk to Your Database: Why Hybrid API + NL2SQL Wins
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  1. DZone
  2. Coding
  3. JavaScript
  4. Custom Container Component in React Native With Different Content and Style

Custom Container Component in React Native With Different Content and Style

By 
Artun Kutluk user avatar
Artun Kutluk
·
Jan. 09, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
22.2K Views

Join the DZone community and get the full member experience.

Join For Free

In React Native (and also in React) you can create custom components and use them easily as much as you want in your app. You may give custom styles and values to these components by passing different values in the props of these components. However, you can not change the child components of your custom components since they are already defined.

In some cases, you may want to have containers that look alike but also have different content and differ in style. At this point, custom container components come in.

Let's create a custom container with the name, "BoxContainer." 

JavaScript
 




xxxxxxxxxx
1


 
1
import React from 'react';
2
import { StyleSheet, View} from 'react-native';
3
 
          
4
const BoxContainer = props => {
5
    return (
6
    );
7
};
8
 
          
9
export default BoxContainer;



We want to add style to our container. The style we will give to our custom container component will be the common style in every usage of our component. So, let's modify our code to return a view with some style. 

JavaScript
 




xxxxxxxxxx
1
24


 
1
import React from 'react';
2
import { StyleSheet, View} from 'react-native';
3
 
          
4
const BoxContainer = props => {
5
    return (
6
        <View style={styles.boxContainer}>
7
        </View>
8
    );
9
};
10
 
          
11
const styles = StyleSheet.create({
12
    boxContainer:{
13
        shadowColor: '#000',
14
        shadowOffset: { width: 0, height: 2 },
15
        shadowOpacity: 0.8,
16
        shadowRadius: 2,
17
        height:200,
18
        margin: 20,
19
        alignItems: 'center',
20
        justifyContent: 'center'
21
    }
22
});
23
 
          
24
export default BoxContainer;


Since our component will be a container, it will contain different components when used. We need to pass these components to the container. This can be achieved with React's "children" prop.

JavaScript
 




x
3


 
1
{props.children}        
8
    boxContainer:{



We also want to pass custom style to the container. As you know, this can be passed by props.style. The problem here that must be handled is that we must combine the common style built in the component and the style passed to the component during usage. We must also allow the common style to be overwritten by the style passed during usage. We can achieve this with the spread operator in JavaScript.

JavaScript
 




x
1


 
1
{...styles.boxContainer, ...props.style}
5
const BoxContainer = props => {



With the help of the spread operator, the style values of boxContainer and style are combined and passed up as a new list. 

JavaScript
 




xxxxxxxxxx
1
26


 
1
import React from 'react';
2
import { StyleSheet, View} from 'react-native';
3
 
          
4
 
          
5
const BoxContainer = props => {
6
    return (
7
        <View style={{...styles.boxContainer, ...props.style}}>
8
            {props.children}
9
        </View>
10
    );
11
};
12
 
          
13
const styles = StyleSheet.create({
14
    boxContainer:{
15
        shadowColor: '#000',
16
        shadowOffset: { width: 0, height: 2 },
17
        shadowOpacity: 0.8,
18
        shadowRadius: 2,
19
        height:200,
20
        margin: 20,
21
        alignItems: 'center',
22
        justifyContent: 'center'
23
    }
24
});
25
 
          
26
export default BoxContainer;



Now, our custom container component is ready to be used. Let's use it with different content and style and see the result.

JavaScript
 




xxxxxxxxxx
1
54


 
1
import React from 'react';
2
import { Button, StyleSheet, Text, View } from 'react-native';
3
import BoxContainer from './components/BoxContainer';
4
5
export default function App() {
6
  return (
7
8
    <View style={styles.page}>
9
      <BoxContainer style={styles.container1}>
10
          <Text>Box Content1</Text>
11
      </BoxContainer>
12
13
      <BoxContainer style={styles.container2}>
14
          <Text>Box Content2</Text>
15
      </BoxContainer>
16
17
      <BoxContainer style={styles.container3}>
18
          <View style={styles.button}>
19
            <Button title='Button' color='black'/>
20
          </View>
21
      </BoxContainer>
22
    </View>
23
  );
24
};
25
26
const styles = StyleSheet.create({
27
  page:{
28
    flex:1,
29
    paddingTop:50
30
  },
31
  
32
  container1: {
33
    backgroundColor: 'yellow',
34
    height : 100
35
  },
36
37
  container2: {
38
    backgroundColor: 'red',
39
    height : 100
40
  },
41
42
  container3: {
43
    backgroundColor: 'grey',
44
    height : 100
45
  },
46
47
  button:{
48
    width:80,
49
    height:40,
50
    color: 'white',
51
    backgroundColor: 'white'
52
  }
53
});
54




And the result:

Final output


Further Reading

  • Object-Oriented JavaScript.
Container React Native React (JavaScript library) JavaScript

Opinions expressed by DZone contributors are their own.

Related

  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • Cross-Platform Mobile Application Development: Evaluating Flutter, React Native, HTML5, Xamarin, and Other Frameworks
  • Creating a React Native SDK: Step-By-Step Guide With Practical Examples
  • ReactJS for AI and Machine Learning: A Powerful Combination

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!