खोज…


परिचय

शैलियों को JSON ऑब्जेक्ट में सीएसएस में समान स्टाइलिंग विशेषता नामों के साथ परिभाषित किया गया है। इस तरह की वस्तु को या तो किसी घटक के स्टाइल प्रोप में इनलाइन डाला जा सकता है या इसे फ़ंक्शन StyleSheet.create(StyleObject) किया जा सकता है और एक वर्ग के समान चयनकर्ता नाम का उपयोग करके कम इनलाइन एक्सेस के लिए एक चर में संग्रहीत किया जा सकता है। सीएसएस में।

वाक्य - विन्यास

  • <Component style={styleFromStyleSheet} />
  • <Component style={styleObject} />
  • <Component style={[style1,style2]} />

टिप्पणियों

अधिकांश रिएक्टिव नेटिव स्टाइल उनके सीएसएस फॉर्म हैं, लेकिन ऊंट मामले में। तो, text-decoration textDecoration बन textDecoration

सीएसएस के विपरीत, शैलियों को विरासत में नहीं मिलता है। यदि आप चाहते हैं कि बाल घटक एक निश्चित शैली के वारिस हों, तो आपको इसे बच्चे को स्पष्ट रूप से प्रदान करना होगा। इसका मतलब है कि आप संपूर्ण View लिए एक फ़ॉन्ट परिवार सेट नहीं कर सकते।
इसका एक अपवाद Text घटक है: नेस्टेड Text उनके मूल शैलियों को विरासत में मिला है।

इनलाइन शैलियों का उपयोग करते हुए स्टाइलिंग

प्रत्येक प्रतिक्रियाशील मूल घटक एक style प्रोप ले सकता है। आप इसे सीएसएस-शैली शैली गुणों के साथ जावास्क्रिप्ट ऑब्जेक्ट पास कर सकते हैं:

<Text style={{color:'red'}}>Red text</Text>

यह अक्षम हो सकता है क्योंकि इसे हर बार घटक को प्रस्तुत करने के लिए वस्तु को फिर से बनाना होगा। स्टाइलशीट का उपयोग करना पसंद किया जाता है।

एक स्टाइलशीट का उपयोग कर स्टाइल

import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';

const styles = StyleSheet.create({
    red: {
        color: 'red'
    },
    big: {
        fontSize: 30
    }
});

class Example extends Component {
    render() {
        return (
            <View>
                <Text style={styles.red}>Red</Text>
                <Text style={styles.big}>Big</Text>
            </View>
        );
    }
}

StyleSheet.create() एक ऑब्जेक्ट देता है जहाँ मान नंबर हैं। React Native इन संख्यात्मक आईडी को सही स्टाइल ऑब्जेक्ट में बदलना जानता है।

कई शैलियों को जोड़ना

आप कई शैलियों को लागू करने के लिए style प्रोप में एक सरणी पास कर सकते हैं। जब कोई संघर्ष होता है, तो सूची में अंतिम एक मिसाल कायम करता है।

import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';

const styles = StyleSheet.create({
    red: {
        color: 'red'
    },
    greenUnderline: {
        color: 'green',
        textDecoration: 'underline'
    },
    big: {
        fontSize: 30
    }
});

class Example extends Component {
    render() {
        return (
            <View>
                <Text style={[styles.red, styles.big]}>Big red</Text>
                <Text style={[styles.red, styles.greenUnderline]}>Green underline</Text>
                <Text style={[styles.greenUnderline, styles.red]}>Red underline</Text>
                <Text style={[styles.greenUnderline, styles.red, styles.big]}>Big red underline</Text>
                <Text style={[styles.big, {color:'yellow'}]}>Big yellow</Text>
            </View>
        );
    }
}

सशर्त स्टाइलिंग

<View style={[(this.props.isTrue) ? styles.bgcolorBlack : styles.bgColorWhite]}>

यदि isTrue का मूल्य true तो इसमें काले रंग की पृष्ठभूमि का रंग अन्यथा सफेद होगा।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow