I have a text string some of words are having # or @ as prefix (in start of words, just as #example or @example) i want to change such words into blue color . React Native
Advertisement
Answer
You can use a custom component like below.
JavaScript
x
18
18
1
const CustomText = (props) => {
2
const text = props.text.split(' ');
3
return <Text>{text.map(text => {
4
if (text.startsWith('@') || text.startsWith('#')) {
5
return <Text style={{ color: 'blue' }}>{text} </Text>;
6
}
7
return `${text} `;
8
})}</Text>;
9
}
10
11
export default function App() {
12
return (
13
<View style={styles.container}>
14
<CustomText text="this is a @sample #text"/>
15
</View>
16
);
17
}
18
You can check the working snack https://snack.expo.io/@guruparan/demo2