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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

  • GenAI Security Gateway: An End-to-End Architecture
  • Spring AI on Local LLMs
  • LangChainGo vs Genkit Go: Where Genkit Shines
  • One Database, Two Models: Building AI-Friendly Java Applications With MySQL JSON Duality Views

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jan. 09, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
22.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook