selcukctn
selcukctn

Reputation: 255

How can I use the variable here in another file? (React-Native)

How can I use the 'const a' variable here in another file?

import React, { Component } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, Animated, Dimensions } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import colorsdata from '../../assets/data/colorsdata';

const { width, height } = Dimensions.get("window");

const a = colorsdata[0].word

Upvotes: 0

Views: 54

Answers (2)

Rifat
Rifat

Reputation: 1888

Export it. Like this:

import React, { Component } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, Animated, Dimensions } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import colorsdata from '../../assets/data/colorsdata';

const { width, height } = Dimensions.get("window");

const a = colorsdata[0].word

export  { a };

Then import it. Like this:

import { a } from "./Path_or_FileName"

Upvotes: 1

Arthur Crocquevieille
Arthur Crocquevieille

Reputation: 11

You can export a variable like so

export const a = colorsdata[0].word

to make it available outside its file. Then you can use it in another file by importing it using your file's path:

import { a } from "./path-to-file"

Upvotes: 1

Related Questions