[英]Export an instance to external component with react
I have a configuration component in react and I need to export the instance to another external component, in this case the: const appInstance我在反应中有一个配置组件,我需要将实例导出到另一个外部组件,在这种情况下是: const appInstance
export const setupApp = () => {
if (isAppAvailable) {
try {
{...}
const appConfiguration = new AppConfiguration(KEY);
AppConfig.configure(appConfiguration);
// I need to export this const
const appInstance = AppConfig.start(startInstance);
} catch (e) {
console.error(e);
}
}
};
And in other component, I need to take this instance, like this:在其他组件中,我需要使用这个实例,如下所示:
// get app instance from another page
appInstance.get(myTest)
.then((value) => {
if (...) {
something
} else {
something;
}
});
How can I do this?我怎样才能做到这一点?
Your function setupApp
currently has no return value.您的 function
setupApp
目前没有返回值。 If you add appInstance
to the return, you can access it from outside the function.如果将
appInstance
添加到返回中,则可以从 function 外部访问它。
export const setupApp = () => {
if (isAppAvailable) {
try {
{...}
const appConfiguration = new AppConfiguration(APP_KEY);
AppConfig.configure(appConfiguration);
// I need to export this const
const appInstance = AppConfig.start(startInstance);
return {
appConfiguration,
appInstance,
}
} catch (e) {
console.error(e);
}
}
};
// get app instance from another page
const appInstance = setupApp().appInstance
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.