我是新来的react 和react 的本地人.目前,对于每个组件,我将代码分解为两个单独的文件:

  1. index.js表示所有React代码,以及;
  2. 样式表为styles.js

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

例子:

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

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

styles.js:

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

上面的代码不起作用,但更多的是为了让我明白我要做的事情.非常感谢您的帮助!

推荐答案

创建一个以iconColor和iconSize为参数并返回样式表对象的类

// 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')
}

React-native相关问答推荐

try 使用JWT-DECODE解码令牌时出错

@testing-library/react-native -> 渲染 -> TypeError:无法读取未定义的属性(读取存在)

React Native:如何以编程方式对图像进行黑白过滤?

React Native Axios 通过数组的键过滤

使用 React Native 下载数据文件以供离线使用

如何在 React Native 中正确地将组件导入到我的导航器中?

reactnavigation后退按钮的自定义后退导航

redux-observable 您在预期流的地方提供了 undefined

React 从 babel 6 到 babel 7 的原生升级

react-native app.js index.js

为什么 Android Studio 强制使用 Android 支持库中的 Androidx?

Fragment片段内的react-native

React Native STUCK 启动打包器

使用来自另一个页面的 fetch 调用函数返回结果值

Firebase 3.3 实时数据库坚持使用 React Native 0.32 Making a connection attempt

在 FlatList 上使用 React Native 搜索过滤器

React Native如何将this.setState更改传递给父级

React Native:如何将 的高度和宽度设置为 容器?

在 React Native 中将props传递给外部样式表?

移动应用程序不需要 CORS?