【发布时间】:2020-11-30 09:44:27
【问题描述】:
谁能帮忙把这个类组件代码转换成功能组件/钩子
class MarkerTypes extends React.Component {
constructor(props) {
super(props);
this.state = {
mapSnapshot: null,
};
}
takeSnapshot() {
this.map.takeSnapshot(
300,
300,
{
latitude: LATITUDE - SPACE,
longitude: LONGITUDE - SPACE,
latitudeDelta: 0.01,
longitudeDelta: 0.01 * ASPECT_RATIO,
},
(err, data) => {
if (err) {
console.log(err);
}
this.setState({ mapSnapshot: data });
}
);
}
render() {
return (
<View>
<MapView initialRegion={...} ref={map => { this.map = map }}>
<Marker coordinate={this.state.coordinate} />
</MapView>
<Image source={{ uri: this.state.mapSnapshot.uri }} />
<TouchableOpacity onPress={this.takeSnapshot}>
Take Snapshot
</TouchableOpacity>
</View>
);
}
像这样:我想点击一个按钮来拍摄快照
https://github.com/react-native-community/react-native-maps#take-snapshot-of-map
const snapShot = () => {
takeSnapshot({
width: 300, // optional, when omitted the view-width is used
height: 300, // optional, when omitted the view-height is used
format: "png", // image formats: 'png', 'jpg' (default: 'png')
quality: 0.8, // image quality: 0..1 (only relevant for jpg, default: 1)
result: "file", // result types: 'file', 'base64' (default: 'file')
});
snapshot.then((uri) => {
console.log(uri);
});
};
return(
<TouchableOpacity onPress={snapShot}>
<AppText>Get snapshot</AppText>
</TouchableOpacity>
)
【问题讨论】:
-
当然,您具体需要什么帮助?
-
我开始学习用钩子做出反应,但没有使用基于类的组件,所以我真的不知道从哪里开始。基本上我现在并不担心状态,我只想在点击按钮时能够使用此代码调用函数。我已经更新了我的问题。我现在不担心状态
标签: reactjs react-native