Javascript 如何从类组件导出函数?自然反应
我有一个类组件,我需要将它的一个函数传递给抽屉并使用它,但不知道是否可能:Javascript 如何从类组件导出函数?自然反应,javascript,react-native,android-studio,Javascript,React Native,Android Studio,我有一个类组件,我需要将它的一个函数传递给抽屉并使用它,但不知道是否可能: 类编辑\u注释扩展组件{ 保存更改=异步()=>{ 清除_content=this.state.content.replace(//g,“”;//replace al 试一试{ const data=JSON.parse(等待AsyncStorage.getItem(“数据”); const index_to_find=this.array_notes.findIndex(obj=>obj.note_number==th
类编辑\u注释扩展组件{
保存更改=异步()=>{
清除_content=this.state.content.replace(//g,“”;//replace al
试一试{
const data=JSON.parse(等待AsyncStorage.getItem(“数据”);
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=清除内容;
返回{…注意}
});
this.array_notes.splice(索引_to_find,1,编辑的_note[0]);
data.array\u notes=this.array\u notes;
等待AsyncStorage.setItem(“数据”,JSON.stringify(数据));
}捕获(错误){
警报(错误);
}
}
render(){
返回(
你好
);
}
}
导出默认编辑注释;
这是我的类组件,我想导出函数save_changes并在编辑注释的标题中使用它,将它放在图标中
import React,{Component}来自“React”;
从“react native”导入{Text,View};
从“@expo/vector icons”导入{Feather};
类头扩展组件{
render(){
返回(
{this.props.title}
);
}
}
导出默认标题;
我怎么能那样做?你不能那样做。唯一的方法是从类中提取
将更改保存为实用程序,导出它并在需要传递正确参数的地方使用它
在包含类组件的文件中,进行以下更改:
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;
export const save\u changes=async()=>{
清除_content=this.state.content.replace(//g,“”;//replace al
试一试{
const data=JSON.parse(等待AsyncStorage.getItem(“数据”);
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=清除内容;
返回{…注意}
});
this.array_notes.splice(索引_to_find,1,编辑的_note[0]);
data.array\u notes=this.array\u notes;
等待AsyncStorage.setItem(“数据”,JSON.stringify(数据));
}捕获(错误){
警报(错误);
}
}
类编辑注释扩展组件{
render(){
返回(
你好
);
}
}
导出默认编辑注释;
现在,您可以从此文件的任何位置导入保存更改
函数要从组件导出函数,您需要将其设置为公共静态
,并删除函数中对此
的所有使用。public
这个词意味着可以通过Edit\u note.save\u changes()
从组件外部访问该函数。关键字static
用于使函数独立于组件实例
要从实例中导入所需的数据,可以将它们作为参数传递到函数save\u changes
因此,您应该有如下内容:
公共静态异步保存更改(内容、数组注释、注释){
清除\u content=content.replace(//g,“”;//replace al
试一试{
const data=JSON.parse(等待AsyncStorage.getItem(“数据”);
const index_to_find=array_notes.findIndex(obj=>obj.note_number===note[0]。note_number);
const edited_note=note.map((note)=>{
note.content=清除内容;
返回{…注意}
});
数组注释.拼接(索引到查找,1,编辑注释[0]);
data.array\u notes=数组\u notes;
等待AsyncStorage.setItem(“数据”,JSON.stringify(数据));
}捕获(错误){
警报(错误);
}
}
然后,您可以使用Edit\u note.save\u changes()
在任何其他组件中调用此函数,只要组件Edit note
也导入到组件的顶行。我是如何做到这一点的?嗯,这样我就不能使用类中的变量了,您需要从函数返回数据,并将其设置在类组件中。这就是您编写代码的方式。如果类不是React组件,那么这将是一种更好的方法。否则,为什么不创建一个简单的类并在需要的地方实例化它并调用函数呢?