【问题标题】:Collect data at mian component of helper class functions return value in React Native在 React Native 中帮助类函数返回值的 mian 组件收集数据
【发布时间】:2022-11-23 21:21:01
【问题描述】:

我制作了一个辅助类,其中包含许多要从不同组件调用的函数,但我无法从调用辅助函数的主组件获取返回值?请帮忙 ..

    helper1: function(){
     console.log("ok Pressed");
    },
    checkIfRoomExist: function(param1, param2){
        console.log("userid", param2);
          firestore()
            .collection('userlist')
            .doc(param2)
            .get()
            .then(documentSnapshot => {
                console.log("room_id=>", documentSnapshot.data().room_id)
            })
            
    },
    helper3: function(param1, param2){

    }
}

export default helpers; `
 This is my helper class and i call it from my components and like
`import helper from './helper';`
Then Call the function and pass params like
`onPress={() => helper.checkIfRoomExist(param1, param2)}`
and now here i want the result of the checkIfRoomExist() function , how ?

【问题讨论】:

  • 请添加一些示例或代码。
  • 先生,我编辑问题请看一下..请帮助..

标签: reactjs react-native


【解决方案1】:

这是功能组件的例子

helper1: function(){
     console.log("ok Pressed");
return "some_data"
    }

    checkIfRoomExist: function(param1, param2){
        console.log("userid", param2);
          firestore()
            .collection('userlist')
            .doc(param2)
            .get()
            .then(documentSnapshot => {
                console.log("room_id=>", documentSnapshot.data().room_id)
            })
            
    }
    helper3: function(param1, param2){

    }

// this is how you can export multiple function from a compoenent
export {helper1,checkIfRoomExist,helper3}


// This is my helper class and i call it from my components and like
import {helper1,checkIfRoomExist,helper3} from './helper';

onPress={async () => let my_return_data = await helper1()}
onPress={() => checkIfRoomExist(param1, param2)}
onPress={() => helper3(param1,parami2)}

//Class Component Examples

class helper {

   helper1: function(){
     console.log("ok Pressed");
    }

    checkIfRoomExist: function(param1, param2){
        console.log("userid", param2);
          firestore()
            .collection('userlist')
            .doc(param2)
            .get()
            .then(documentSnapshot => {
                console.log("room_id=>", documentSnapshot.data().room_id)
            })
            
    }
    helper3: function(param1, param2){

    }

}
// this is how you can export multiple function from a compoenent
export default helper


// This is my helper class and i call it from my components and like
import helper from './helper';

 
onPress={() => new helper().helper1()}
onPress={() => new helper().checkIfRoomExist(param1, param2)}
onPress={() => new helper().helper3(param1,parami2)}

【讨论】:

  • 我想你不能明白我的意思是什么?您只需复制粘贴代码...我质疑如何从主要组件获取返回数据?
  • 你想从这些函数中返回一些数据吗??
  • 是的,我想在调用辅助函数的主组件访问返回的数据...怎么样,先生?
  • 好的。我已经更新了答案。您必须返回一些数据并使用 async 和 await 来获取该数据。
猜你喜欢
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多