Forgive me if I'm missing something standard here, as React isn't my default javascript dev tool. This seems like something that should fairly easily obtainable though so I figured I should ask. Is it possible, and if so how can I find a substring, and then change the style of that substring in React Native?
Example:
render() {
return (
<View>
<Text>
{this.state.myText}
</Text>
</View>
);
}
componentDidMount() {
setTimeout(() => {
let newStr = this.state.myText.replace('green', '<Text style={{ color: "green" }}>green</Text>');
this.setState({ myText: newStr });
},
3000)
}
But clearly you can't dynamically add a Text component in order to style a substring this way. What is the proper method?
Edit:
I've been looking into this more, and if I'm not mistaken... I have to make an entire array of subcomponents, keep track of the subcomponents, and then dynamically add/remove text components to this array along with the strings if I simply want to change the color of text? Someone please tell me React is not this flawed in this aspect...
Final code used:
render() {
return (
<View style={styles.container}>
<Text>
{this.state.textItems.map(item => (
<Text style={{ color: item === 'green' ? 'green' : 'black' }}>{item}</Text>
))}
</Text>
</View>
);
}
componentDidMount() {
setTimeout(() => {
this.setState({ textItems: this.state.textItems[0].split((/(green)/)) });
},
3000)
}