Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing props into external stylesheet in React Native?

Tags:

I'm new to React and React Native. At the moment for each component I'm breaking the code into 2 separate files:

  1. index.js for all the React code, and;
  2. styles.js for the StyleSheet

Is there a way to pass props into the external StyleSheet?

Example: index.js:

render() {
  const iconColor = this.props.color || '#000';
  const iconSize = this.props.size || 25;

  return (
    <Icon style={styles.icon} />
  );
}

Example styles.js:

const styles = StyleSheet.create({
  icon : {
    color: iconColor,
    fontSize: iconSize
  }
});

The code above does not work, but it's more there just to get the point across of what I'm trying to do. Any help is much appreciated!

like image 697
chapeljuice Avatar asked Mar 09 '17 23:03

chapeljuice


People also ask

Can I use props in StyleSheet React Native?

With React Native, you style your application using JavaScript. All of the core components accept a prop named style . The style names and values usually match how CSS works on the web, except names are written using camel casing, e.g. backgroundColor rather than background-color .

How do you pass props to native component?

After we import the PresentationalComponent and pass it to the render function, we need to pass the props. We will pass the props by adding myText = {this. state. myText} and deleteText = {this.

How do you pass styles as props React Native TypeScript?

Use the React. CSSProperties type to pass CSS styles as props in React TypeScript, e.g. style: React. CSSProperties; . The CSSProperties type is used to type the style object that consists of CSS property names and values.


3 Answers

I rather to have my styles in a separate file styles.js. Inside styles.js:

export const styles = (props) => StyleSheet.create({         icon : {         color: props.iconColor,         fontSize: props.iconSize       }     } 

Inside your main class you can pass the value

return (     <Icon style={styles(this.props).icon} />   ); 

Alternatively you can those value directly so it would be

export const styles = (iconColor,iconSize) => StyleSheet.create({     icon : {     color: iconColor,     fontSize: iconSize   } } 

and inside your main class

return (     <Icon style={styles(this.props,iconColor,  this.props.iconSize).icon} />  ); 
like image 96
Kevin Avatar answered Sep 18 '22 12:09

Kevin


i'm sending noFooter boolean prop in a style sheet

   <View style={styles.mainFooterCont(noFooter)}>
     <Text> Testing </Text>
    </View>

and receiving it like

  mainFooterCont: noFooter => ({
   flexDirection: 'row',
   justifyContent: 'space-between',
   alignItems: 'flex-end',
   paddingBottom: noFooter ? 0 : 20,
   paddingTop: Metrics.ratio(noFooter ? 0 : 5),
   }),
like image 35
Syed Amir Ali Avatar answered Sep 18 '22 12:09

Syed Amir Ali


Create a class that takes iconColor and iconSize as arguments and returns a StyleSheet object

// styles.js

export default class StyleSheetFactory {
    static getSheet(iconSize, iconColor) {
        return StyleSheet.create({
            icon : {
                color: iconColor,
                fontSize: iconSize
            }
        })
    }
}

// index.js

render() {
    let myStyleSheet = StyleSheetFactory.getSheet(64, 'red')
}
like image 29
FuzzyTree Avatar answered Sep 16 '22 12:09

FuzzyTree