What if you could fix a bug or change a style once and see it everywhere instantly?
Why Custom components in React Native? - Purpose & Use Cases
Imagine building a mobile app where you need the same button style on many screens. You copy and paste the button code everywhere, changing colors or text each time.
This manual way is slow and risky. If you want to change the button style later, you must find and update every copy. It's easy to miss some, causing inconsistent looks and bugs.
Custom components let you create a reusable button once. Then you use it everywhere by just calling its name. Change the button style in one place, and all buttons update automatically.
const Screen1 = () => <Button color="blue" title="Click" />; const Screen2 = () => <Button color="blue" title="Submit" />;
const MyButton = ({title}) => <Button color="blue" title={title} />;
const Screen1 = () => <MyButton title="Click" />;
const Screen2 = () => <MyButton title="Submit" />;Custom components make your app easier to build, update, and keep looking great everywhere.
Think of a shopping app where every product card looks the same. Using a custom product card component means you design it once and reuse it for all products, saving time and keeping the app consistent.
Manual repetition causes slow updates and errors.
Custom components let you reuse UI parts easily.
One change updates all uses, saving time and effort.
Practice
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 DQuick Check:
Custom component = reusable function [OK]
- Confusing components with styles
- Thinking components are variables
- Mixing built-in libraries with custom components
Greeting in React Native?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 AQuick Check:
Function component must return JSX with return [OK]
- Omitting return in arrow functions
- Assigning JSX directly to variables
- Returning strings instead of JSX
function Welcome(props) {
return Hi, {props.name}!;
}
// Usage:
<Welcome name="Anna" />Solution
Step 1: Understand props usage
The component receives a prop called 'name' and inserts it inside the Text element using curly braces.Step 2: Substitute the prop value
Since the component is used with name="Anna", the output will be 'Hi, Anna!'.Final Answer:
Hi, Anna! -> Option BQuick Check:
Props insert values inside JSX [OK]
- Treating props as plain text
- Forgetting curly braces for props
- Assuming props are undefined
function Button() {
return (
<TouchableOpacity>
<Text>Click me</Text>
</TouchableOpacity>
)
}
export default Button;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 CQuick Check:
All used components must be imported [OK]
- Forgetting to import React Native components
- Thinking Text can't be inside TouchableOpacity
- Confusing function naming rules
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?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 AQuick Check:
Destructure props and add styles for reusable UI [OK]
- Not using props to customize content
- Forgetting to style reusable components
- Using literal strings instead of props
Ready to prove it?
Pass the test and this pattern moves unknown
