Is it possible to split var styles = StyleSheet.create from React.component into different scripts in the native answer?

Is it possible to split var styles = StyleSheet.create from React.component into another script in the native response?

+5
source share
3 answers

it is possible. just create a js file with this template:

'use strict'; var React = require('react-native'); var myStyles = React.StyleSheet.create({ style1: { }, style2: { } )} module.exports = myStyles; 

then in your js component you need to use this stylesheet for example. if your js style file is called phongyewtong.js

 var s = require('../the/path/to/phongyewtong'); 

using:

 <View style = {s.style1} /> 
+12
source

Both of the links below explain very well how to move styles from your "structural" code:

Basically (copying the code fragment from the link above 2) your styles have in a separate JS, say, text.js file:

 const text = StyleSheet.create({ p: { color: 'black', fontFamily: 'Open Sans', fontSize: 14, }, title: { fontWeight: 'bold', color: 'black', fontFamily: 'Open Sans', fontSize: 20, } }); export default text; 

In the React component, you can simply import this text style and use it directly

 <Text style={text.p}>settings</Text> 

Hope this helps.

+2
source

In a later version of React Version (0.31), I used this code:

 import React, { Component, PropTypes } from 'react'; import { StyleSheet } from 'react-native'; var styles = StyleSheet.create({ ... }); module.exports = styles; 
0
source

Source: https://habr.com/ru/post/1232884/


All Articles