[英]Issue importing values from one file to another in react-native
I am learning ReactNative, and now I'm looking into how to organize the files (for now I am going to create a folder for each page, each with an index, functions and styles file). 我正在学习ReactNative,现在我正在研究如何组织文件(目前,我将为每个页面创建一个文件夹,每个页面都有一个索引,函数和样式文件)。 For some reason, though, I am not being able to import information to
index.js
, can't use the styles or functions, when I open the page, it doesn't return the func method. 但是由于某种原因,我无法将信息导入
index.js
,无法使用样式或函数,因此当我打开页面时,它不会返回func方法。
I am wondering whether I am using import wrong. 我想知道我是否使用导入错误。 Using
import { MenuFunctions} from './Menu'
has resulted in an error saying nothing was imported, this error no longer appears, but nothing is being returned still. import { MenuFunctions} from './Menu'
使用import { MenuFunctions} from './Menu'
导致出现错误,提示未导入任何内容,该错误不再出现,但仍未返回任何内容。
This is my code, index, Menu and styles are all in the same folder. 这是我的代码,索引,菜单和样式都在同一文件夹中。
index.js
import React from 'react';
import MenuFunctions from './Menu';
import MenuStyles from './styles'
import { Text, View } from 'react-native';
export default class MenuPage extends React.Component {
render(){
return(
<View>
<Text> Text: {MenuFunctions.func} </Text>
</View>
);
}
}
Menu.js
import React from 'react';
export default class MenuFunctions extends React.Component{
constructor(props){
super(props);
}
func = () => {return "Hello, World!"};
}
styles.js
import React from 'react';
import { StyleSheet } from 'react-native';
export default class MenuStyles extends React.Component{
styles = StyleSheet.create({
text: {
color: "red",
}
});
}
Menu.js
and styles.js
shouldn't be React.Component
and you forgot to call to Func
method, ()
is missing. Menu.js
和styles.js
不应为React.Component
并且您忘记了调用Func
方法,缺少()
。
React.Component
is a UI component only which include a render method that returns JSX
element. React.Component
是一个UI组件,仅包含返回JSX
元素的render方法。
Your code should look like that: 您的代码应如下所示:
index.js
import React from 'react'; import MenuFunctions from './Menu'; import MenuStyles from './styles'; import {Text, View} from 'react-native'; export default class MenuPage extends React.Component { render() { return ( <View> <Text> Text: {MenuFunctions.func()} </Text> </View> ); } }
Menu.js
import React from 'react'; class MenuFunctions { func = () => { return 'Hello, World!'; }; } export default new MenuFunctions();
styles.js
import {StyleSheet} from 'react-native'; export default styles = StyleSheet.create({ text: { color: Colors.red30, } });
The problem is that you are trying to import Menu.js
using import MenuFunctions from './Menu'
when you had to specify the file itself: './Menu/Menu.js'
. 问题在于,当您必须指定文件本身:
'./Menu/Menu.js'
import MenuFunctions from './Menu'
时,您正在尝试使用import MenuFunctions from './Menu'
import Menu.js
import MenuFunctions from './Menu'
(remember to call the function using parentheses class.function()
) (请记住使用括号
class.function()
调用该函数)
Also, whenever you export as default you don't have to use curly braces {}
另外,每当您默认导出时,都不必使用花括号
{}
If you are wondering about your project structure, you can use the following as a common structure to create projects. 如果您对项目结构有所疑问,可以将以下内容用作创建项目的通用结构。 Let's say you have the following
假设您有以下情况
- index.js
- src
- assets
- screens
- MenuScreen
- components
- services
- menu
- index.js //this is Menu.js
- android
- ios
NOTE 注意
Do not extend React.Component
if you are not exporting a component. 如果不导出组件,请不要扩展
React.Component
。
You need an object to perform the function of the class you created. 您需要一个对象来执行您创建的类的功能。
Then declare and use the constructor
. 然后声明并使用
constructor
。
index.js index.js
import React from 'react';
import MenuFunctions from './Menu';
import MenuStyles from './styles'
import { Text, View } from 'react-native';
export default class MenuPage extends React.Component {
constructor(props){
super(props);
this.menu = new MenuFunctions()
MemuStyle = new MenuStyles()
}
render(){
return(
<View>
<Text style={MemuStyle.styles.text}> Text: {this.menu.func()}</Text>
</View>
);
}
}
Menu.js Menu.js
import React from 'react';
export default class MenuFunctions extends React.Component {
func(){
return 'Hello, World!';
}
}
styles.js styles.js
import { StyleSheet } from 'react-native';
export default class MenuStyles extends React.Component {
styles = StyleSheet.create({
text: {
color: "red",
}
});
}
NOTE : The features you intend to use do not necessarily have to inherit
React
.注意 :您打算使用的功能不一定必须继承
React
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.