在React Native中将道具传递到外部样式表?

cha*_*ice 9 javascript jsx reactjs react-jsx react-native

我是React和React Native的新手.目前,对于每个组件,我将代码分成两个单独的文件:

  1. index.js 对于所有React代码,和;
  2. styles.js 对于StyleSheet

有没有办法将道具传递到外部StyleSheet?

示例 index.js::

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

  return (
    <Icon style={styles.icon} />
  );
}
Run Code Online (Sandbox Code Playgroud)

示例styles.js:

const styles = StyleSheet.create({
  icon : {
    color: iconColor,
    fontSize: iconSize
  }
});
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用,但更重要的是要了解我正在尝试做的事情.任何帮助深表感谢!

Sye*_*Ali 15

我在样式表中发送 noFooter boolean prop

   <View style={styles.mainFooterCont(noFooter)}>
     <Text> Testing </Text>
    </View>
Run Code Online (Sandbox Code Playgroud)

并接收它

  mainFooterCont: noFooter => ({
   flexDirection: 'row',
   justifyContent: 'space-between',
   alignItems: 'flex-end',
   paddingBottom: noFooter ? 0 : 20,
   paddingTop: Metrics.ratio(noFooter ? 0 : 5),
   }),
Run Code Online (Sandbox Code Playgroud)

  • 这似乎确实有效,但打字稿会抱怨。 (8认同)
  • 这是迄今为止最好、最干净的答案。只有需要 props 的样式才需要处理它们。 (3认同)

Kev*_*vin 9

我宁愿将样式放在单独的文件styles.js中。内部styles.js:

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

在主类中,您可以传递值

return (
    <Icon style={styles(this.props).icon} />
  );
Run Code Online (Sandbox Code Playgroud)

或者,您可以直接将这些值设为

export const styles = (iconColor,iconSize) => StyleSheet.create({
    icon : {
    color: iconColor,
    fontSize: iconSize
  }
}
Run Code Online (Sandbox Code Playgroud)

在你的主班里

return (
    <Icon style={styles(this.props,iconColor, 
this.props.iconSize).icon} />
 );
Run Code Online (Sandbox Code Playgroud)

  • 使用此方法是否存在任何性能缺陷,因为这将启动箭头函数? (2认同)
  • @JohnStuart“性能缺点”与典型的内联样式完全相同:每次组件渲染时都会创建一个新的样式对象,因为每次组件渲染时都会调用 stylesheet.create 。但实际上,它不太可能有一点影响。 (2认同)

Fuz*_*ree 7

创建一个以iconColor和iconSize作为参数的类,并返回一个StyleSheet对象

// 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')
}
Run Code Online (Sandbox Code Playgroud)


Cat*_*rta 5

只需将样式表包装在一个函数中,您可以选择在其中传递道具。

代替:

const styles = StyleSheet.create({
  Title: { color: 'white' }
});
Run Code Online (Sandbox Code Playgroud)

你做:

const styles = (props?: any) => StyleSheet.create({
  Title: { color: 'white' }
});
Run Code Online (Sandbox Code Playgroud)

现在当你将它们添加到你的组件中时,而不是

style={styles.Title}
Run Code Online (Sandbox Code Playgroud)

你做:

style={styles(propsObjectHere).Title}
Run Code Online (Sandbox Code Playgroud)

由于这是可选的并且您没有道具可以通过,只需执行以下操作:

style={styles().Title}
Run Code Online (Sandbox Code Playgroud)

PS 如果您出于某种原因不使用 TypeScript,请忽略该类型:P