Let's say there are 10 <Text></Text>
components in a row. These components will be created by looping over an array like this:
...
const fontsize = 16
return (
<View style={{flex: 1}}>
{
array.map((val, index, arr) => {
return (
<Text ref={'text' + index} style={{fontSize: fontsize}]}>{val}</Text>
)
})
}
</View>
)
...
Now I want to change the fontSize of the <Text>
component with the refs of 'text5'
I know I can get/set the style of this component with this.refs.text5.props.style.fontSize
but how I can update the virtual DOM?
To change button style on press in React Native, we can wrap our button with the TouchableHighlight component. to define the touchProps object that we use to set the props of TouchableHighlight . We set onHideUnderlay to a function that runs when the underlay is hidden.
React Native lets you style your whole application using JavaScript. Every component can use a prop named style , which enables you to write CSS styles for those components.
Use state: https://facebook.github.io/react-native/docs/state.html
Calling this.setState
will re-render the view with the updated state.
e.g.
class Example extends Component {
constructor(props) {
super(props);
this.state = {
fontizes: [16, 127, 2, ...]
};
}
setFontsize(size, index) {
let current = this.state.fontsizes;
current[index] = size;
this.setState({ fontsizes: current });
}
render() {
return (
<View style={{flex: 1}}>
{
array.map((val, index, arr) => {
return (
<Text ref={'text' + index} style={{fontSize: this.state.fontsizes[index]}]}>{val}</Text>
)
})
}
</View>
)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With