如何访问外部js文件的道具

时间:2020-01-18 18:48:10

标签: reactjs

我已经看到对这种情况的答案都涉及一个类组件。 我在React中有一个功能组件。

const MyComponent = props => {
// all my code
// here I can execute props.firebase.someother stuff
}

如上所述,我有一个props.firebase,它链接到firebase上下文。这使我可以调用firebase。它有效,而且一切都很好。 为了使我的代码更整洁,我想在外部js文件中进行一些此类调用。

我想做:

import { myMethod } from "./helpers/allcalls.js";

In my allcalls.js
export function setRecords(data) {
props.firebase.makecall.set(data)
}

const MyComponent = props => {
// all my code
// here I can execute props.firebase.someother stuff
 setRecords(someDataFromState)
}

在上面的道具是不确定的。 在一个类中,我将其绑定到(this),然后在外部js中使用this.props.firebase。我不知道如何在功能组件中做到这一点。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

此强大的帮助:@AlexW&@Claeusdev

道具是一个对象,可以传递给任何外部方法。

    import { myMethod } from "./helpers/allcalls.js";

const MyComponent = props => {
// all my code
// here I can execute props.firebase.someother stuff
 setRecords(props, someDataFromState)
}

在我的allcalls.js中

export function setRecords(data) {
props.firebase.makecall.set(data)
}

上面的代码是不完整的,应该假定是props,并且所有其他导入的React等都将包括在内。