如何在react-native中的其他组件中调用函数

我正在开发一个棋盘游戏,该游戏使用很长的代码向棋盘生成数据。

目前我有2个组成部分。家庭和董事会。主页只有一个欢迎消息和一个用于导航到Board的按钮。

当前,Board具有生成数据的功能,但我想将此功能移至其他组件,在那里生成数据,并将数组返回到我的Board组件。这个新功能/组件除了将数组返回给Board之外,不会做其他任何事情。

可以在React-Native中进行吗?我来自其他编程逻辑,我不知道该如何实现。

谢谢。

shanyecai 回答:如何在react-native中的其他组件中调用函数

您可以在React Native,latency.cthis link for documentation

中使用道具 ,

使用参考

Class A extends React.Component{
      returnArray = () => return ['a','b','c'];

      render(){....}
};

在其他班级

Class B extends React.Component{ 
     getArray = () => {
         const array = this.comp.returnArray(); //do whatever you want to do with the array
     }
     render(){
        return <A ref={ref=>this.comp=ref}/>
     }
}
,

在功能组件中,您可以导出功能,例如:

SELECT customers.*,sum(CASE
             WHEN debit_items.customer_id = customers.id THEN
              debit_items.debit_amount
             ELSE
              0
           END) as total_debit,sum(CASE
             WHEN credit_items.debit_id = debit_items.id THEN
              credit_items.credit_amount
             ELSE
              0
           END) as total_credit
  FROM customers
  LEFT JOIN debit_items
    ON customers.id = debit_items.customer_id
   AND debit_items.deleted = '0'   
  LEFT JOIN 
    (SELECT SUM(credit_amount) as credit_amount,debit_id from credit_items GROUP BY debit_id) as credit_items
        ON debit_items.id = credit_items.debit_id
   AND credit_items.deleted = '0'
 GROUP BY customers.id

在您的电路板组件中,您可以从js文件导入功能,并像这样使用它。

export const arrayFunction = () => {
// array generation code
   return array
}

此常量数组将具有从函数返回的数组。

,

也许我最好的建议是,您需要使用redux来管理多个组件上的功能和状态,并且可以在文档的任何位置(全局)使用redux,可以在以下位置看到它:Reudux Official

本文链接:https://www.f2er.com/3162425.html

大家都在问