1

アプリ全体で使用できる主な色に2つの変数を設定します。コードは次のとおりです。これらの2つの変数を反応ネイティブで一緒にエクスポートするには

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

const primary = '#27bcb5'; 
const secondary = '#ffffff; 

これらの2つの変数を一緒にエクスポートして、アプリケーションコンポーネントで使用するにはどうすればよいですか。溶液で

問題は次のとおりです。

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

const primary = '#27bcb5'; 
const secondary = '#ffffff'; 

export const DefaultStyle = { 
    primary, 
    secondary, 
} 

だからあなたは、それらを使用することができます。

import {DefaultStyle} from './variables'; 

const screenHeight = Dimensions.get("window").height; 

const styles = { 
    wrapper: {}, 
    slide1: { 
    flex: 1, 
    justifyContent: "center", 
    alignItems: "center", 
    backgroundColor: primary, 

それはあなたがそれらをエクスポートすることができ、変数の主

答えて

1

を見つけることができません言います:

import { DefaultStyle } from './default-variables'; 
console.log(DefaultStyle.primary); 
console.log(DefaultStyle.secondary); 
+0

返信いただきありがとうございます。私はあなたのソリューションを試しましたが、変数を他のコンポーネントで使用しようとすると、変数primaryを見つけることができません。上記の使用例を追加しました。オブジェクトアイテムとして使用する必要がありますか? backgroundColor:Defaultstyles.primary – Artur

+0

'backgroundColor:DefaultStyle.primary' – Val

+0

変数' primary'を使いたい場合、@ TimHの答えはうまくいくはずです。 – Val

1

名前付きエクスポートを実行できます。例えば。 :、私はより良い何かをしたいと

import { primary, secondary } from 'YOUR_FILE_PATH' 
0

あなたは瞬間のように保存することができますグローバルファイル()の時間、デバイスの対策などを定義します。

その後
import React, { Component } from 'react'; 
import { View, StyleSheet } from 'react-native'; 

const primary = '#27bcb5'; 
const secondary = '#ffffff'; 

export { primary, secondary }; 

、あなたは経由であなたのスタイルをインポートすることができます。

global.jsあなたのコード内の

const global = { 
    PRIMARY: '#27bcb5', 
    SECONDARY: '#ffffff, 
    DEVICE_MEASURES:{ 
    WIDTH: Dimensions.get("window").width, 
    HEIGHT: Dimensions.get("window").height; 
    }, 
    ... 
} 

export default global 

そして:

import GLOBAL from '(directory)/global' 

<View style={{ width: GLOBAL.DEVICE_MEASURES.WIDTH }}> 
    <Text style={{ color: GLOBAL.PRIMARY }}> 
    sup 
    </Text> 
</View> 

アプリの設定をすべて保存できるグローバルファイルを作成する方が良いでしょう。

関連する問題