admin管理员组文章数量:1332388
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 ponent when present in children? 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.
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 ponent when present in children? 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.
2 Answers
Reset to default 3Just pass text or icon as props to your Button
Component
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
ponents.
You can use 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 ponent. It will turn both passed down icon and text grey.
本文标签: javascriptReact Native styling propschildrenStack Overflow
版权声明:本文标题:javascript - React Native: styling props.children - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742290319a2447675.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论