javascript - 将道具传递给子组件 - React Native
问题描述
我想将道具传递color
给图标孩子。这个<Feather />
我想添加color
为道具
这是我的组件和羽毛孩子
import { Feather } from '@expo/vector-icons'
export default class CloseButton extends React.PureComponent {
render () {
const { style, ...props } = this.props
return (
<TouchableOpacity style={styles.close} link {...props} >
<Feather name='x' size={26} /> // <-- want add color here
</TouchableOpacity>
)
}
}
这是我发送显示的道具的组件ThouchableOpacity
<Alert.CloseButton onPress={props.onRequestClose} />
如何在这个道具中传递颜色并且它只显示在图标上?
解决方案
您可以使用color
为CloseButton
您传递给Feather
组件的组件调用的道具。
export default class CloseButton extends React.PureComponent {
render () {
const { style, color, ...props } = this.props;
return (
<TouchableOpacity style={styles.close} link {...props} >
<Feather name='x' size={26} color={color} />
</TouchableOpacity>
)
}
}
用法
<Alert.CloseButton
onPress={props.onRequestClose}
color="red"
/>
推荐阅读
- r - 具有生成回归器的 2SLS,第一阶段校正
- laravel - Laravel - local.ERROR:ErrorException:不应静态调用非静态方法 Illuminate\Database\Eloquent\Model::update()
- swift - 快速导入私有框架
- mysql - 从 MySQL 中的嵌套 JSON 中提取值
- linux - 非root用户如何在特权端口上运行
- javascript - 如何从 Firestore 中的多层文档中获取字段
- java - 有没有办法指定用于在maven中编译依赖项目的JDK版本?
- powershell - 在文件夹中查找最新文件
- amazon-web-services - 在 AWS 中设置 Datapipeline 流
- javascript - 如何在ejs中设置if else条件