javascript - How to I call a Vanillas JS function that is within a another Vanillas JS function, in React? - Stack Overflow

I have a React component that is sitting within a normal web page (so not a SPA).There is a Vanilla JS

I have a React component that is sitting within a normal web page (so not a SPA).

There is a Vanilla JS function, within another JS function, on this webpage I wish to call from within my React component. This function looks like:

const getInfo = (() => {
  const updateInfo = (dataId) => {
    //Do something
  }
  const showInfo = (dataId) => {
    //Do something
  }
  return {updateInfo, showInfo}
})();

And they're called on the (normal) webpages like:

getInfo.updateInfo("1324");

Now, I want to include this in my React component. So, I created a helper function, simply copying the above.

const getInfo = (() => {
  const updateInfo = (dataId) => {
    //Do something
  }
  const showInfo = (dataId) => {
    //Do something
  }
  return {updateInfo, showInfo}
})();

And calling it from within another helper function, as normal:

const calculate = () => {
   getInfo.updateInfo("1324");
   // Other React helper function stuff
}

However, when I try to compile I get the error

getInfo.updateInfo is not a function

Would anyone know how I can use getInfo.updateInfo(); within my React component's helper functions?

I have a React component that is sitting within a normal web page (so not a SPA).

There is a Vanilla JS function, within another JS function, on this webpage I wish to call from within my React component. This function looks like:

const getInfo = (() => {
  const updateInfo = (dataId) => {
    //Do something
  }
  const showInfo = (dataId) => {
    //Do something
  }
  return {updateInfo, showInfo}
})();

And they're called on the (normal) webpages like:

getInfo.updateInfo("1324");

Now, I want to include this in my React component. So, I created a helper function, simply copying the above.

const getInfo = (() => {
  const updateInfo = (dataId) => {
    //Do something
  }
  const showInfo = (dataId) => {
    //Do something
  }
  return {updateInfo, showInfo}
})();

And calling it from within another helper function, as normal:

const calculate = () => {
   getInfo.updateInfo("1324");
   // Other React helper function stuff
}

However, when I try to compile I get the error

getInfo.updateInfo is not a function

Would anyone know how I can use getInfo.updateInfo(); within my React component's helper functions?

Share Improve this question asked Dec 5, 2024 at 6:50 MeltingDogMeltingDog 15.3k52 gold badges178 silver badges319 bronze badges 3
  • Where did you create the helper function getInfo with respect to the React component? What i see here is copying of code whereas you can attach this to the global scope and then use it. Mostly global scope would be window – Tushar Shahi Commented Dec 5, 2024 at 7:27
  • Can you log getInfo? – Tushar Shahi Commented Dec 5, 2024 at 7:30
  • Please provide a minimal reproducible example. – jabaa Commented Dec 5, 2024 at 7:48
Add a comment  | 

2 Answers 2

Reset to default 1

One of the ways to do this is to attach this to the window object. In your vanilla js script:

const getInfo = (() => {
  const updateInfo = (dataId) => {
    //Do something
  }
  const showInfo = (dataId) => {
    //Do something
  }
  const getInfoObject = {updateInfo, showInfo};
  window.getInfo = getInfoObject;
  return window.getInfo;
})();

And now window.getInfo is available for anyone to use. You can use it in your React component.

Unfortunately, you haven't provided complete information about the structure of your component, so it's likely that no one will be able to give you a definitive answer. If this function is defined outside the component, it must either be imported directly or passed to the component as props to be accessed. If you're using a helper function, you need to ensure it receives the required parameters; otherwise, React won't recognize what getInfo.updateInfo() is and will throw an error.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1736148365a3864527.html

相关推荐

  • 【连网】Win10总是自动断网,检测默认网关不可用

    1、简介 最近发现电脑会时不时自动断网(频繁掉线),且十分频繁,一天有七八次,咨询了身边人他们都没有这个问题。于是开启了各种方法

    2小时前
    20
  • 鸿蒙NEXT版仿华为阅读App的逐页浏览PDF

    上一篇文章虽然使用PdfView组件及其控制器实现了PDF文档的加载功能,但实际的渲染结果会把PDF的所有页面都显示出来,而非由用户控制的一页一页浏览。那么若想实现更加精细的浏览操作,就要引入PDF服务模块pdfService了。pdfSe

    2小时前
    20
  • JetBrains IDEs GO AI:最新coding agent,更智能的编程助手!

    大家好,欢迎来到程序视点!我是你们的老朋友.小二!JetBrains AI现在,AI 几乎无处不在。作为一名资深码农,我一直期待着有更智能、更高效的工具来提高开发的工作效率,并为开发带来更多乐趣,从而让自己从枯燥的CRUD中解放出来!从 2

    1小时前
    10
  • 蜜罐检测

    核心功能功能模块技术实现URL去重输入文件哈希去重+结果文件增量校验智能限速令牌桶算法(支持动态时序调整)并发控制ThreadPoolExecutor线程池管理异常处理7大类错误分类统计(含QUIC协议错误) 蜜罐识别Cookie检测+备注

    1小时前
    00
  • 鸿门宴讲PostgreSQL

    最近有点忙,被一个老师联系,说周日紧急救场。说是有一个大央企要做一节PostgreSQL的课,PPT都写好了,就让我去讲一讲就可以了。我这人好面子,紧急救场去吧,也没想太多。从此有意思的故事就开始了,因为要伪装成这家委托我企业的员工,资深的

    1小时前
    00
  • RAG 作者:RAG 已死,RAG 万岁!

    Datawhale分享 作者:Douwe Kiela,编译:思考机器本文作者 Douwe Kiela,RAG 论文(Retrieval-Augmented Generation for Knowledge-Intensive NLP Ta

    1小时前
    00
  • REST API 还是 GraphQL?

    前言说到 API 设计,GraphQL 和 RESTful API 是当前最主流的两种选择,各自有其独特优势与挑战。有幸的是,我上家公司的时候,使用的就是 GraphQL,而在上上家使用的是 RESTful API,今天我就结合我的经验,来

    1小时前
    00
  • 面试题:read 返回0是否代表一定对方连接已经关闭?

    坚持思考,就会很酷。大家好,这是进入大厂面试准备 第1篇文章知识地图:LINUX系统调用问:read 返回0是否一定表示对端关闭?答:​​普通文件​​:当读取位置到达文件末尾(EOF)时,返回 0(跟设置 阻塞和非阻无关)管道套接字​​:

    1小时前
    00
  • 手把手教你使用 mcp

    手把手教你使用 mcp-server —— vscode + 多款 MCP Server 实现高德地图旅游攻略生成并分享给其他人Author:GoritDate:2025年4月24日目标:看完这篇文章你就能学会如何使用调用高德地图 MC

    1小时前
    00
  • minio使用简介

    在云原生和微服务时代,对象存储已成为存储非结构化数据(如图片、日志、备份等)的首选方案。MinIO 是一款高性能、兼容 S3 API 的开源对象存储服务,而它的官方 Go SDK —— minio-go,则可以让你在 Go 语言项目中轻松集

    1小时前
    00
  • 高效开发必备!小程序组件复用的实用技巧

    高效开发必备!支付宝小程序组件复用的实用技巧

    59分钟前
    00
  • 业内首次! 全面复现DeepSeek

    机器之心发布机器之心编辑部OpenAI 的 o1 系列和 DeepSeek-R1 的成功充分证明,大规模强化学习已成为一种极为有效的方法,能够激发大型语言模型(LLM) 的复杂推理行为并显著提升其能力。然而,这些推理模型的核心训练方法在其技

    52分钟前
    00
  • Rust 现状全揭示:开发者生产力提升,信心却在下滑?

    作者 | Tim Anderson 译者 | Sambodhi 策划 | Tina图片2024 年 Rust 现状调查结果已经发布,显示生产力有所提升,但编译慢和调试困难仍然是重大挑战。另一个令人关注的问题是,尽管 Rust

    42分钟前
    00
  • .NET 9版本支持说明

    在深入探讨.NET 9库的激动人心改进前,有必要了解微软对.NET版本的支持策略。• 奇数版本(如.NET 9):属于标准期限支持(STS),提供18个月支持周期,适合尝试前沿功能。• 偶数版本(如.NET 8或未来的.NET 10):提供

    27分钟前
    00
  • 华为将大规模推出AI芯片,助力替代英伟达H100,打破限制!

    据路透社昨日凌晨独家报道,华为预计最早将在5月份推出大量910C AI芯片,部分产品已经开始出货。此次华为发布的新产品主要是为了应对美国对国内AI芯片的限制,助力国内企业缓解AI芯片供应紧张的问题。值得注意的是,本月美国政府要求英伟达销售H

    24分钟前
    00
  • 深入微服务核心:从架构设计到规模化

    《Building Microservices》这本书是吃透微服务的大部头,本文基于全书内容,系统性地阐述了微服务架构的设计原则、实施策略与挑战,从微服务的核心概念出发,延伸到架构设计、服务拆分、集成技术及规模化实践,为开发者提供了构建稳健

    19分钟前
    00
  • MVVMHabit

    过去属于死神,未来属于你自己。 ——英国谚语 MVVMHabit:基于 MVVM 的快速开发框架在 Android 开发中,架构设计是影响项目质量和维护成本的重要因素。为了帮助开发者快速构建高质量、易维护的应用,MVVMHabit 提供了

    12分钟前
    00
  • 如何搭建一个高效安全的API开放平台:完整步骤指南

    在当今数字化时代,API(应用程序编程接口)已成为连接不同系统和服务的桥梁。一个设计良好的API开放平台能够为企业带来巨大的商业价值和技术优势。本文将详细介绍从零开始搭建一个API开放平台的完整步骤,涵盖技术选型、架构设计、安全防护和运维管

    10分钟前
    00
  • JS8Core

    7分钟前
    00
  • linux命令详细说明以及案例

    参考资料linux查看进程linux解压ziplinux详细说明以及案例centos7镜像下载详细说明以及案例centos 8详细说明以及案例centos 安装docker详细说明以及案例HTTP,TCP,UDP常见端口对照表centos

    4分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信