Custom components help you reuse code and build your app faster. They let you create your own building blocks for the app's look and behavior.
You want to show the same button style in many places.
You need a special header that appears on multiple screens.
You want to organize your app into smaller, easy-to-understand parts.
You want to add custom behavior to a group of UI elements.
You want to keep your code clean and avoid repeating yourself.
Syntax
React Native
function MyComponent(props) {
return (
<View>
<Text>{props.title}</Text>
</View>
);
}
Custom components are just functions that return UI elements.
You can pass data to them using props.
Examples
A simple component that shows a greeting.
React Native
function Greeting() {
return <Text>Hello!</Text>;
}
This component uses a prop to show a personalized greeting.
React Native
function Greeting(props) {
return <Text>Hello, {props.name}!</Text>;
}
Same as above but using arrow function and destructuring props.
React Native
const Greeting = ({ name }) => <Text>Hello, {name}!</Text>;
Sample App
This app shows two cards using the CustomCard component. Each card has a title and description. The cards have simple styling and accessibility labels.
Always give your components clear names that describe what they do.
Use props to make components flexible and reusable.
Remember to add accessibility labels for better app usability.
Summary
Custom components let you build reusable UI pieces.
They are simple functions that return UI elements.
Use props to pass data and customize components.
Practice
(1/5)
1. What is a custom component in React Native?
easy
A. A CSS style sheet
B. A built-in React Native library
C. A special type of variable
D. A reusable function that returns UI elements
Solution
Step 1: Understand what a component is
A component is a piece of UI that can be reused in different parts of the app.
Step 2: Identify custom components
Custom components are user-defined functions that return UI elements, unlike built-in libraries or styles.
Final Answer:
A reusable function that returns UI elements -> Option D
Quick Check:
Custom component = reusable function [OK]
Hint: Custom components are functions returning UI [OK]
Common Mistakes:
Confusing components with styles
Thinking components are variables
Mixing built-in libraries with custom components
2. Which of the following is the correct syntax to define a simple custom component named Greeting in React Native?
easy
A. function Greeting() { return Hello; }
B. var Greeting = Hello;
C. const Greeting = () => { Hello }
D. class Greeting extends Component { render() { return 'Hello'; } }
Solution
Step 1: Check function component syntax
Function components return JSX inside the function body with a return statement.
Step 2: Evaluate each option
function Greeting() { return Hello; } correctly defines a function returning JSX. var Greeting = Hello; assigns JSX directly to a variable, which is invalid. const Greeting = () => { Hello } misses the return statement. class Greeting extends Component { render() { return 'Hello'; } } uses class syntax but returns a string, not JSX.
Final Answer:
function Greeting() { return Hello; } -> Option A
Quick Check:
Function component must return JSX with return [OK]
Hint: Function components need a return statement [OK]
Common Mistakes:
Omitting return in arrow functions
Assigning JSX directly to variables
Returning strings instead of JSX
3. What will be displayed on the screen when this component is rendered?
A. Text component cannot be inside TouchableOpacity
B. Function must be named with uppercase first letter
C. Missing import of TouchableOpacity
D. Missing return statement
Solution
Step 1: Check component usage
The component uses TouchableOpacity and Text, which must be imported from 'react-native'.
Step 2: Identify missing import
The code snippet does not show imports, so the likely error is missing import of TouchableOpacity.
Final Answer:
Missing import of TouchableOpacity -> Option C
Quick Check:
All used components must be imported [OK]
Hint: Always import components you use [OK]
Common Mistakes:
Forgetting to import React Native components
Thinking Text can't be inside TouchableOpacity
Confusing function naming rules
5. You want to create a reusable Card component that accepts a title and content as props and displays them inside styled View and Text components. Which of the following is the best way to define this component?
hard
A. function Card({ title, content }) {
return (
{title}
{content}
);
}
B. function Card(props) {
return (
{props.title}
{props.content}
);
}
C. function Card() {
return (
title
content
);
}
D. function Card({ title, content }) {
return (
{title}
);
}
Solution
Step 1: Check props usage and destructuring
function Card({ title, content }) {
return (
{title}
{content}
);
} uses destructuring to get title and content props, which is clear and concise.
Step 2: Verify UI structure and styling
function Card({ title, content }) {
return (
{title}
{content}
);
} wraps texts in a styled View with padding, border, and rounded corners, making it reusable and visually distinct.
Step 3: Compare with other options
function Card(props) {
return (
{props.title}
{props.content}
);
} lacks styling, while the option without props uses literal strings instead of props, and the option missing content display omits the content Text.
Final Answer:
function Card({ title, content }) {
return (
{title}
{content}
);
} -> Option A
Quick Check:
Destructure props and add styles for reusable UI [OK]
Hint: Destructure props and add styles for reusable components [OK]
Common Mistakes:
Not using props to customize content
Forgetting to style reusable components
Using literal strings instead of props
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.