简体   繁体   English

在React Native中将样式值从文件传递到另一个

[英]Pass a style value from a file to another one in react native

I am a beginner in react and I am trying to give a style value from a file to another. 我是React的初学者,我正在尝试将文件中的样式值提供给另一个文件。

This is Square.js : 这是Square.js:

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Numbers from './Numbers';

export default class Square extends React.Component {
    render() {
      return (
        <View style={squareStyles.square}>
          <Numbers number={this.props.squareNumber}></Numbers>
        </View>
      );
  }
}

const squareStyles = StyleSheet.create({
  square: {
    width: 100,
    height: 100,
    flex: 1,
    backgroundColor: 'skyblue',
    borderWidth: 3,
    borderColor: 'black',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

This is App.js 这是App.js

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Square from './Square';

export default class App extends React.Component {
  render() {
    let flexDirection = StyleSheet.flatten(styles.image).flexDirection;
    return (
      <View>
        <Text style={styles.title}>{flexDirection}</Text>
        <View style={styles.imageContainer}>
            <Text style={styles.flexboxTitle}>flexbox</Text>
            <View style={styles.image}>
              <Square squareNumber='1'></Square>
              <Square squareNumber='2'></Square>
              <Square squareNumber='3'></Square>
            </View>
        </View>
      </View>
    );
  }
}

function ucFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

const styles = StyleSheet.create({
  title: {
    alignItems: 'center',
    textAlign: 'center',
    fontWeight : 'bold',
    fontSize: 20,
    marginTop: 30
  },
  imageContainer : {
    alignItems: 'center',
    marginTop: 30,
    marginRight: 20,
    marginLeft: 20,
    padding: 20,
    borderWidth: 4,
    borderColor: 'black',
    height: 400
  },
  flexboxTitle : {
    marginBottom: 20
  },
  image : {
    flex: 1,
    flexDirection : 'row',
    height: 800,
    paddingTop : 80
  }
})

I would like to get the value of square backgroundColor from Square.js and import it in App.js. 我想从Square.js获取square backgroundColor的值,并将其导入App.js。

I tried to declare variable with StyleSheet.flatten but I don't really know where to declare it and how to give it to App.js. 我试图用StyleSheet.flatten声明变量,但我真的不知道在哪里声明它以及如何将其赋给App.js。

Thank you for your help. 谢谢您的帮助。

@R.Duteil has the right idea. @ R.Duteil有正确的主意。 In this case it's definitely best to keep your style const in a seperate file (maybe under a StyleSheet folder) and export/import as needed. 在这种情况下,绝对最好将样式常量保存在单独的文件中(可能在StyleSheet文件夹下),并根据需要导出/导入。 Also - your question mentioned you wanted to pass Square styles to App component. 另外-您提到的问题是您想将Square样式传递给App组件。 Passing props from child to parent is a complicated question with React with many solutions (they call it "lifting state up"). 对于React来说,通过许多解决方案将道具从孩子传递到父母是一个复杂的问题(他们称其为“提升状态”)。 There's much to read on this topic, but none are particularly useful for passing styles around components. 关于这个主题,有很多文章可供阅读,但是对于在组件之间传递样式而言,没有什么特别有用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM