Reputation: 895
The Goal: To create a reusable react component and have the flexibility to style any part of the component in the future.
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?
Upvotes: 0
Views: 4338
Reputation: 895
The Goal: To create a reusable react component and have the flexibility to style any part of the component in the future.
How to achieve this:
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. 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>
)
}
Upvotes: 2