简体   繁体   English

如何从类组件导出函数? 反应本机

[英]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(/&nbsp;/g,""); //replace al &nbsp; 

    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(/&nbsp;/g,""); //replace al &nbsp; 
    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.

相关问题 如何从无渲染类 React-Native 调用函数 - How to call function from renderless class React-Native React-native:使用其他组件中的“ this”导出功能 - React-native: Export function using 'this' from other component React-Native如何从另一个类的构造函数中的函数获取数组 - React-Native How to get an array from a function in the constructor of another class 如何使用DrawerNavigation导出类并将其导入React-native中的另一个类? - How to export a class using DrawerNavigation and import it in another class in React-native? 函数或类之外的“超级”-react-native - 'super' outside of function or class - react-native 元素类型无效:应为字符串(用于内置组件)或类/函数(用于复合组件),但得到:object。 React-Native - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. React-Native 分享 React-Native 组件 - Share React-Native components 如何从共享组件文件夹导入不同的应用程序? 反应 / 反应原生 - How can different apps import from a shared components folder? react / react-native 如何从异步函数中提取输出-react-native? - How to extract output from async function - react-native? 如何从 react-native 中的 function 返回数据 - How to return data from function in react-native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM