javascript - 如何在本机反应中将无状态函数转换为类组件
问题描述
我是 react native 的新手,我一直在寻找如何将此函数转换为 react native 中的类组件。请我需要帮助将下面的代码转换为反应组件。
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>
);
解决方案
要将其转换为类组件,只需将代码移动到类组件的 render 方法中,并将对 的引用更改为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>
)
}
}
推荐阅读
- c++ - 了解 compare_exchange_weak
- amazon-web-services - AWS EC2 - 我可以使用公共 DNS 主机名完成哪些我无法使用公共 IPv4 地址的事情?
- android - 放大 Auth 和 Android:如何获取访客会话令牌?
- java - 如何将文本添加到 JTextArea
- google-app-engine - 在自定义 Flex GAE 中挂载 Google Cloud Storage
- node.js - 节点和 npm cli 不起作用
- javascript - 按钮 ID 为 getElementById
- html - 没有 CSS - html 两个 div 并排
- css - Angular - 我如何将 Api 中的图像设置为背景图像
- azure - Azure 持久编排功能不存在、已禁用或不是编排功能