![](/img/trans.png)
[英]How can I convert this Class component function into functional component in React Native
[英]How can I convert stateless function to class component in react native
我是 react native 的新手,我一直在尋找如何在 react native 中將這個 function 轉換為 class 組件。 請我需要幫助將下面的代碼轉換為反應組件。
import React from 'react';
import { View, Image, ScrollView } from 'react-native';
import styles from './styles';
export default ({captures=[]}) => (
<ScrollView
horizontal={true}
style={[styles.bottomToolbar, styles.galleryContainer]}
>
{captures.map(({ uri }) => (
<View style={styles.galleryImageContainer} key={uri}>
<Image source={{ uri }} style={styles.galleryImage} />
</View>
))}
</ScrollView>
);
要將其轉換為 class 組件,只需將代碼移動到 class 組件的渲染方法中,並將對props
的引用更改為對this.props
的引用。 對於此組件,無需進行其他更改。
export default class Example extends React.Component {
render () {
const { captures = [] } = this.props;
return (
<ScrollView
horizontal={true}
style={[styles.bottomToolbar, styles.galleryContainer]}
>
{captures.map(({ uri }) => (
<View style={styles.galleryImageContainer} key={uri}>
<Image source={{ uri }} style={styles.galleryImage} />
</View>
))}
</ScrollView>
)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.