简体   繁体   English

如何将 Style Props 传递到 React Native 中的可重用组件中,以便将来更灵活的样式

[英]How To Pass Style Props into a reusable component in React Native for more flexible styling in the future

The Goal: To create a reusable react component and have the flexibility to style any part of the component in the future.目标:创建一个可重用的 React 组件,并在未来灵活地为组件的任何部分设置样式。

for example, I create a button-like component that I want to reuse in different cases in the future and give it different styles in the different places that I use it:例如,我创建了一个类似按钮的组件,我想在未来的不同情况下重用它,并在我使用它的不同地方赋予它不同的样式:

function reusableButton(){
return(
<View style={styles.defaultStyle}>
<Text style={styles.defaultTitleStyle}>Button Title</Text>
</View>
)}

const styles = StyleSheet.create({
  defaultStyle:{
 height: 50,
 width: 100,
 borderRadious: 25,
 backgroundColor:'red'},

 defaultTitleStyle: {
color:'green',
fontWeight: 'bold'}
})

Question is: How do I make changes to the default styles in the future when I use this button?问题是:当我使用这个按钮时,将来如何更改默认样式?

The Goal: To create a reusable react component and have the flexibility to style any part of the component in the future.目标:创建一个可重用的 React 组件,并在未来灵活地为组件的任何部分设置样式。

How to achieve this:如何实现这一点:

  1. Create the reusable component using const or function.使用 const 或 function 创建可重用组件。
  2. Pass 'props' as a parameter to the component, whether functional or const.将“props”作为参数传递给组件,无论是函数式还是常量。
  3. Give the elements of your reusable component stylings that are arrays of the default styling and "props.futureStyle".提供可重用组件样式的元素,它们是默认样式和“props.futureStyle”的数组。 Note that "futureStyle" here is just a name and you can use any name you want.请注意,这里的“futureStyle”只是一个名称,您可以使用任何您想要的名称。
  4. Whenever you call the reusable component, you can easily use "futureStyle" to make any changes.每当您调用可重用组件时,您都可以轻松地使用“futureStyle”进行任何更改。 Note that you can declare "futureStyle1", "futureStyle2" etc to the different parts of the reusable components so that you can edit the styles of every View, Text, etc that is part of the components wherever they may be added in the future请注意,您可以将“futureStyle1”、“futureStyle2”等声明到可重用组件的不同部分,以便您可以编辑作为组件一部分的每个视图、文本等的样式,无论它们将来可能添加到何处

Example: Creating the reusable components:示例:创建可重用组件:

 const ReusableButton = (props) =>{
    return(
    <View style={[styles.currentButtonStyle, props.futureButtonStyle]}>
    <Text style={[styles.currentButtonTitle, props.futureTitleStyle]}>{props.title}</Text>
    </View> )}; 

//Here are my default styles:

const styles = Stylesheet.create({
currentButtonStyle: {
    height:50,
    width: 100, 
    backgrroundColor:'red'},
currentButtonTitle:{
    color: 'white'
    fontSize: 20,
 },
})

Henceforth, anywhere I wish to call and use the ReusableButton, it can edit the styles with the future styles.从此以后,在我希望调用和使用 ReusableButton 的任何地方,它都可以使用未来的样式编辑样式。 Example:例子:

function NewComponent(){
return(
<View>
<Text>Hi let us make use of our old reusable button</Text>
<ReusableButton futureButtonStyle={{width: 200, height: 100, backgroundColor:'blue'}} futureTitleStyle={{fontWeight:'bold', fontSize: 50}}/>
</View>
)
}  

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

相关问题 如何在 React Native 中将道具传递给模态组件 - How to Pass Props to Modal Component in React Native 如何使 React 组件在样式方面可重用? - How to make a React component reusable in terms of styling? 如何在React中以可重用形式传递道具 - How to pass props from a reusable form in React 如何在 React Native 的类组件中将函数作为道具传递和执行? - How to pass and execute functions as props in class Component in React Native? 如何在 Input 组件的 inputContainerStyle 中将 backgroundColor 作为道具传递给本机元素 - How to pass backgroundColor as props in inputContainerStyle in Input component react native elements 如何解构道具以传递给子组件? REACT-NATIVE - How to destructure props to pass for childs component? REACT-NATIVE 将道具传递给子组件 - React Native - Pass props to child component - React Native 无法将 Props 传递给 React Native 中的另一个组件 - Cannot Pass Props to another component in React Native React Native/TypeScript - 如何在功能组件中管理 state 并使用 FlatList 应用灵活的样式 - React Native/TypeScript - How to manage state in functional component and apply flexible style with FlatList React Native:当在子组件上使用spread运算符时,将样式作为props传递的正确方法是什么 - React Native: what is a proper way to pass style as props, when using spread operator on the child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM