简体   繁体   English

React Native:样式 props.children

[英]React Native: styling props.children

Let's imagine I have a Button Component.假设我有一个按钮组件。 I can import it from different screens and it behaves independently.我可以从不同的屏幕导入它,它的行为是独立的。 I can give it different children (text, image, text + icon, icon etc.).我可以给它不同的子项(文本、图像、文本 + 图标、图标等)。

This would look like something like that:这看起来像这样:

import React from 'react';
import { TouchableOpacity } from 'react-native';

import theme from '$/theme';

const styles = StyleSheet.create({
  button: {
    backgroundColor: 'red',
  },
  text: {
    fontFamily: 'Nunito',
    color: 'white',
  },
  icon: {
    color: 'grey',
  }
});

export default (props) => (
  <TouchableOpacity {...props} style={styles.button}>
    {props.children}
  </TouchableOpacity>
);

Then然后

// I want to be able to do either without having to style Text/Icon each time
const button1 = <Button><Text>Click</Text></Button>
const button2 = <Button><Icon name="back" /></Button>

I want to define the style for icon, text and so on directly in the Button Component, right?我想直接在按钮组件中定义图标、文本等的样式,对吗? Is it possible to pass the styles.text variable to the Text component when present in children?当存在于孩子中时,是否可以将styles.text变量传递给 Text 组件? Idem for icon, image and so on?图标、图像等同上吗?

I don't feel like having to fulfill the children style every time is a good practice.我不觉得每次都必须满足孩子们的风格是一种很好的做法。 I could however use <Button text={'Click'} /> syntax then play if/else statements but I am not fond of passing attributes for the content.然而,我可以使用<Button text={'Click'} />语法然后播放 if/else 语句,但我不喜欢为内容传递属性。

Just pass text or icon as props to your Button Component 只需将文本或图标作为道具传递给Button组件

export default (props) => (
  <TouchableOpacity {...props} style={styles.button}>
    {props.title && <Text style={styles.text}>{props.title}</Text>}
    {props.icon && <Icon name={props.icon} />}
  </TouchableOpacity>
);

Alternatively create ButtonText and ButtonIcon components. 或者,创建ButtonTextButtonIcon组件。

You can use React.cloneElement 您可以使用React.cloneElement

export default (props) => (
  <TouchableOpacity {...props} style={styles.button}>
    {this.renderChildren()}
  </TouchableOpacity>
);

renderChildren() {
    return this.props.children.map(children => {
        return React.cloneElement(children, {style: { color: 'grey', fontFamily: 'cochin' }});
    })
}

But that is if you want use the same style regardless of type of component. 但这就是是否要使用相同的样式而不管组件的类型如何。 It will turn both passed down icon and text grey. 它将使向下传递的图标和文本变为灰色。

We have a good Essential cross-platform UI components for React Native that named nativebase.io and it has a easy and complete components . 我们为React Native提供了一个很好的Essential跨平台UI组件,名为nativebase.io ,它具有简单而完整的组件。 with nativebase.io we can make the components like HTML . 使用nativebase.io,我们可以制作HTML之类的组件。 for example this code: 例如此代码:

const styles = StyleSheet.create({
  button: {
    backgroundColor: 'red',
  }
});

export default (props) => (
  <TouchableOpacity {...props} style={styles.button}>
    {props.children}
  </TouchableOpacity>

now it's : 现在是:

<Button><Text>Click</Text></Button> //without any const

the complete components like Tabs Header Body Icon and... exist in nativeBase's Docs. 完整的组件(如Tabs Header Body Icon和...)存在于nativeBase的文档中。

good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM