Inline styling involves directly applying style properties to components. It is useful for dynamic styles that change based on component state or props.
style
prop as an object.Imagine you want to highlight a course card when it is selected. Inline styling allows you to dynamically change the background color based on the card’s state.
import React, { useState } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
const CourseCard = ({ course }) => {
const [selected, setSelected] = useState(false);
return (
<TouchableOpacity
onPress={() => setSelected(!selected)}
style={{
backgroundColor: selected ? '#d1e7dd' : '#fff',
padding: 16,
borderRadius: 8,
marginVertical: 8,
shadowColor: '#000',
shadowOpacity: 0.2,
shadowRadius: 4,
}}
>
<Text style={{ fontSize: 18, fontWeight: 'bold', color: '#333' }}>{course.title}</Text>
<Text style={{ fontSize: 14, color: '#666' }}>{course.description}</Text>
</TouchableOpacity>
);
};
export default CourseCard;
In this lesson, you learned about inline styling and its application in React Native. While it offers flexibility for dynamic styles, it should be used judiciously to maintain code readability and manageability.