मूल रूप से यदि आप आकार सेट करना चाहते हैं और इसे बदलना चाहते हैं तो इसे इस तरह लेआउट पर स्थिति में सेट करें:
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, View } from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'yellow',
},
View1: {
flex: 2,
margin: 10,
backgroundColor: 'red',
elevation: 1,
},
View2: {
position: 'absolute',
backgroundColor: 'orange',
zIndex: 3,
elevation: 3,
},
View3: {
flex: 3,
backgroundColor: 'green',
elevation: 2,
},
Text: {
fontSize: 25,
margin: 20,
color: 'white',
},
});
class Example extends Component {
constructor(props) {
super(props);
this.state = {
view2LayoutProps: {
left: 0,
top: 0,
width: 50,
height: 50,
}
};
}
onLayout(event) {
const {x, y, height, width} = event.nativeEvent.layout;
const newHeight = this.state.view2LayoutProps.height + 1;
const newLayout = {
height: newHeight ,
width: width,
left: x,
top: y,
};
this.setState({ view2LayoutProps: newLayout });
}
render() {
return (
<View style={styles.container}>
<View style={styles.View1}>
<Text>{this.state.view2LayoutProps.height}</Text>
</View>
<View onLayout={(event) => this.onLayout(event)}
style={[styles.View2, this.state.view2LayoutProps]} />
<View style={styles.View3} />
</View>
);
}
}
AppRegistry.registerComponent(Example);
आप इसे कैसे संशोधित कर सकते हैं इसके कई और बदलाव कर सकते हैं, इसे एक अन्य घटक में उपयोग करके, जिसमें आवरण के रूप में एक और दृश्य है और एक ऑनरप्रोडररेल कॉलबैक बनाएं, जो राज्य में टच इवेंट स्थान को पारित कर सकता है, जिसे तब बाल घटक को पारित किया जा सकता है। संपत्ति के रूप में, जो अद्यतन स्थिति को ओवरलैड कर सकता है, जैसे {[styles.View2, this.state.view2LayoutProps, this.props.touchEventTopLeft]}
और रख कर।