简体   繁体   English

React.js:修改所有组件的render()方法?

[英]React.js: modify render() method for all components?

出于调试原因,我想在一般的render()方法中添加以下行,因此它将在所有组件中执行。

console.log('render' + this.constructor.displayName, this.state);

I assume you want to do this without changing any of the existing code. 我假设您希望在不更改任何现有代码的情况下执行此操作。 I played around with this and found a way to do so if you're using something like webpack or browserify to build your application and you're using React v0.13 . 我玩这个并找到了一种方法, 如果你使用webpack或browserify之类的东西来构建你的应用程序并且你正在使用React v0.13

It's important to note that this uses private methods, reaching into React's internals, and could break at any time. 重要的是要注意,这使用私有方法,进入React的内部,并且可以随时中断。 That said, it might be useful for your debugging purposes. 也就是说,它可能对您的调试有用。

[Update to the Update] [更新到更新]

If you use Babel, I highly recommend checking out the React Transform plugin . 如果您使用Babel,我强烈建议您查看React Transform插件 This will let you do all sorts of nifty stuff to React, including wrapping (or overwriting!) render methods. 这将让你为React做各种漂亮的东西,包括包装(或覆盖!) render方法。

[Update] [更新]

I've found a way to do this without hacking into React.addons.Perf ; 我已经找到了一种方法来做到这一点,而不是黑客入侵React.addons.Perf ; the key was the module name of ReactCompositeComponent and the function name of _renderValidatedComponent —just wrap that method to inject your custom behavior. 问题的关键是的模块名称ReactCompositeComponent和功能名称_renderValidatedComponent -只是换行的方法来注入你的自定义行为。

Note you'll need to place this code before you require("react") . 请注意,您需要 require("react") 之前放置此代码require("react")

var ReactCompositeComponent = require("react/lib/ReactCompositeComponent");
var oldRenderValidatedComponent = ReactCompositeComponent.Mixin._renderValidatedComponent;
ReactCompositeComponent.Mixin._renderValidatedComponent = function() {
  var name = this.getName();
  if (name && !name.match(/^ReactDOM/)) {
    console.log("render: ", this.getName(), {props: this._instance.props, state: this._instance.state});
  }
  return oldRenderValidatedComponent.apply(this, arguments);
}

You'll then end up with a very similar result as the old answer, below. 然后,您将得到与下面的旧答案非常相似的结果。 I've added better logging of props and state, and filter out any of the built in ReactDOM* components. 我添加了更好的props和state记录,并过滤掉任何内置的ReactDOM*组件。

例


[Old Answer] [旧答案]

I've overridden the default measure function of the performance tools, which React calls through its codebase to measure performance when using React.addons.Perf . 我已经覆盖了性能工具的默认measure函数,当使用React.addons.Perf时,React通过其代码库调用以测量性能。 By doing so, we're able to get the information that the default measurement strategy would normally get. 通过这样做,我们能够获得默认测量策略通常会获得的信息。 Note that this breaks the normal behavior React.addons.Perf . 请注意,这会破坏React.addons.Perf的正常行为。

Add this code to the entry-point of your application (after you require React): 将此代码添加到应用程序的入口点(在您需要React之后):

var ReactInjection = require("react/lib/ReactInjection");
var ReactDefaultPerf = require("react/lib/ReactDefaultPerf");

ReactDefaultPerf.start();
ReactInjection.Perf.injectMeasure(function measure(moduleName, fnName, fn) {
  return function() {
    if (moduleName === 'ReactCompositeComponent' && fnName === '_renderValidatedComponent') {
      var name = this.getName();
      if (name) {
        console.log("render: ", name);
      }
    }

    return fn.apply(this, arguments);
  }
});

And you'll get the following in your console logs: 您将在控制台日志中获得以下内容:

安慰

ReactElement s with no names (that is, components that make up regular HTML elements like span and div ) are not shown. 没有显示名称的ReactElement (即组成常规HTML元素的组件,如spandiv )未显示。 One notable set of exceptions is button and other input elements, as React provides composite components that wrap those to help manage state. 一组值得注意的例外是button和其他input元素,因为React提供了复合组件来包装它们以帮助管理状态。 They show up as ReactDOMButton and ReactDOMInput . 它们显示为ReactDOMButtonReactDOMInput

React supports Mixins for such cross-cutting concerns: React支持Mixins以解决这些交叉问题:

https://facebook.github.io/react/docs/reusable-components.html#mixins https://facebook.github.io/react/docs/reusable-components.html#mixins

However, it's not permitted to define a render method in a mixin. 但是,不允许在mixin中定义render方法。 The restrictions on each of the React lifecycle methods are in the following source: 每个React生命周期方法的限制在以下来源中:

https://github.com/facebook/react/blob/0c6bee049efb63585fb88c995de788cefc18b789/src/core/ReactCompositeComponent.js#L189 https://github.com/facebook/react/blob/0c6bee049efb63585fb88c995de788cefc18b789/src/core/ReactCompositeComponent.js#L189

If you could assign this behaviour to one of the other steps in the component lifecycle, mixins might work for you. 如果您可以将此行为分配给组件生命周期中的其他步骤之一,则mixins可能适合您。

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

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