简体   繁体   English

从 Chrome devtools 调用组件函数

[英]Invoke component function from Chrome devtools

Apologies if this is a dumb question, but I was wondering if it is at all possible to access a function defined in a React component from the browser?抱歉,这是一个愚蠢的问题,但我想知道是否有可能从浏览器访问 React 组件中定义的函数? For example:例如:

class Test extends React.Component {

  someFn = () => {
    console.log('hey!') 
  }

  render() { return <div>...</div> }
}

So once the website has loaded and the component has been mounted, is it possible to somehow access the javascript and invoke someFn()?因此,一旦网站加载完毕并安装了组件,是否有可能以某种方式访问​​ javascript 并调用 someFn()? I know that, for example, Redux variables exist on the global scope and those can be manipulated, but I'm unsure of class functions.我知道,例如,Redux 变量存在于全局范围内并且可以操作这些变量,但我不确定类函数。 If so, I would assume state objects on these classes to be editable as well, right?如果是这样,我会假设这些类上的状态对象也是可编辑的,对吗?

With React Developer Tools , you can select your component in the tree on the ⚛️Components dev tools tab.使用React Developer Tools ,您可以在 ⚛️Components 开发工具选项卡上的树中选择您的组件。 Once selected, that component instance can be accessed in the console as $r , allowing you to invoke your function with $r.someFn() .选择后,该组件实例可以在控制台中作为$r访问,允许您使用$r.someFn()调用您的函数。

在开发者控制台中输入这行代码:
(new Test()).someFn();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM