Ravi
Ravi

Reputation: 35589

import from base component can't find variable

I have created one BaseComponent which will be extended in all other components :

import React, { Component } from 'react'
import { ScrollView, Text, Image, View, TouchableOpacity, TextInput } from 'react-native'

export default class BaseComponent extends Component {

}

And in my other components i am using it like :

import BaseComponent from '../Components/BaseComponent'

export default class LoginScreen extends BaseComponent {

}

Now i have imported View,ScrollView etc in BaseComponent but when i am trying to use it in child component, it is showing error that can'find variable View.

How can i use imported class of BaseComponnet in ChildComponent?

Upvotes: 1

Views: 241

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 282160

In Nodejs each file is a treated as a module, that has its own scope of variables. When you import variable into file say React or ScrollView for example, you add this variable to the module scope, but not to the global scope.

In case of webpack you can use ProvidePlugin to a few imports as global

new webpack.ProvidePlugin({
  React: 'react' // ReactJS module name in node_modules folder
})

After that you are able to skip importing these variables variable in all of your modules as webpack will do it itself handle it whereever needed.

Upvotes: 2

Related Questions