[英]How to export function from class components? REACT-NATIVE
I have a class component and I need to pass one of its function to the drawer and use it, but don't know if it's possible:我有一个类组件,我需要将它的一个函数传递给抽屉并使用它,但不知道是否可能:
class Edit_note extends Component {
save_changes = async() => {
let clear_content = this.state.content.replace(/ /g,""); //replace al
try {
const data = JSON.parse(await AsyncStorage.getItem("data"));
const index_to_find = this.array_notes.findIndex(obj => obj.note_number === this.note[0].note_number);
const edited_note = this.note.map((note) => {
note.content = clear_content;
return {...note}
});
this.array_notes.splice(index_to_find, 1, edited_note[0]);
data.array_notes = this.array_notes;
await AsyncStorage.setItem("data", JSON.stringify(data));
} catch(error) {
alert(error);
}
}
render() {
return (
<>
<Text>hi</Text>
</>
);
}
}
export default Edit_note;
this is my class component, and I want to export the function save_changes and use it in the header of edit note, put it in the icon这是我的类组件,我想导出函数 save_changes 并在编辑注释的标题中使用它,将其放在图标中
import React, { Component } from "react";
import { Text, View } from "react-native";
import { Feather } from '@expo/vector-icons';
class Header extends Component {
render() {
return (
<>
<View style ={{backgroundColor: "white", flexDirection: "row", alignItems: "center"}}>
<View>
<Text style = {{color: "black", fontSize: 30, marginLeft: -20}}>{this.props.title}</Text>
</View>
<Feather name="check-square" size={24} color = "black" />
</View>
</>
);
}
}
export default Header;
How can I do that?我怎样才能做到这一点?
You can not do that.你不能这样做。 Only way is to extract
save_changes
out of the class as a utility, export it and use it where ever u need passing the right parameters.唯一的方法是从类中提取
save_changes
作为实用程序,将其导出并在需要传递正确参数的任何地方使用它。
In the file where you have your class component, make the following changes:在您拥有类组件的文件中,进行以下更改:
export const save_changes = async(<Pass the parameter here>) => {
let clear_content = this.state.content.replace(/ /g,""); //replace al
try {
const data = JSON.parse(await AsyncStorage.getItem("data"));
const index_to_find = this.array_notes.findIndex(obj => obj.note_number === this.note[0].note_number);
const edited_note = this.note.map((note) => {
note.content = clear_content;
return {...note}
});
this.array_notes.splice(index_to_find, 1, edited_note[0]);
data.array_notes = this.array_notes;
await AsyncStorage.setItem("data", JSON.stringify(data));
}catch(error) {
alert(error);
}
}
class Edit_note extends Component {
render() {
return (
<>
<Text>hi</Text>
</>
);
}
}
export default Edit_note;
Now you can import the save_changes
function anywhere from this file现在您可以从此文件的任何位置导入
save_changes
函数
To export a function from your component, you need it to be public static
, and remove all usage of this
within the function.要从您的组件中导出函数,您需要将其设为
public static
,并在函数中移除this
所有用法。 The public
word means that the function can be accessed from outside the component, with Edit_note.save_changes()
. public
词意味着可以从组件外部访问该函数,使用Edit_note.save_changes()
。 The keyword static
is used to make the function independant of the component's instance.关键字
static
用于使函数独立于组件的实例。
To import the data you need from your instance, you can pass them as parameters in your function save_changes
.要从实例导入所需的数据,您可以将它们作为参数传递到函数
save_changes
。
As a result, you should have something like this:结果,你应该有这样的东西:
public static async save_changes (content, array_notes, note) {
let clear_content = content.replace(/ /g,""); //replace al
try {
const data = JSON.parse(await AsyncStorage.getItem("data"));
const index_to_find = array_notes.findIndex(obj => obj.note_number === note[0].note_number);
const edited_note = note.map((note) => {
note.content = clear_content;
return {...note}
});
array_notes.splice(index_to_find, 1, edited_note[0]);
data.array_notes = array_notes;
await AsyncStorage.setItem("data", JSON.stringify(data));
} catch(error) {
alert(error);
}
}
And then, you can call this function in any other component with Edit_note.save_changes()
, as long as the component Edit-note
is imported as well at the top lines of the component.然后,您可以使用
Edit_note.save_changes()
在任何其他组件中调用此函数,只要组件Edit-note
也在组件的顶行导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.