Skip to content

Circular reference in "bound method" #10476

@iamvery

Description

@iamvery

Do you want to request a feature or report a bug?

Unclear, but I lean towards bug.

What is the current behavior?

Certain "bound" functions create a circular reference which makes traversing objects problematic.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/84v837e9/).

I'm not sure how to produce a small test case for this issue.

What is the expected behavior?

Unsure.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

React 16.0.0-alpha.12
React Native 0.46.4
Jest 20.0.4

This is happening in a test when I attempt to use jsonpath to query the results of renderer.create(<ListItem />).toJSON(). As you can see from that link, I have attempted to address the circular reference in the jsonpath library, but the problem persists. I have managed to track this down to a circular property defined in create-react-class (which is apparently still used by some native components). Here is that snippet of code:

function bindAutoBindMethod(component, method) {
  var boundMethod = method.bind(component);

  boundMethod.__reactBoundContext = component;
  boundMethod.__reactBoundMethod = method;
  boundMethod.__reactBoundArguments = null;

Unfortunately I've yet to narrow that down the precise place this occurs in source as that method definition is repeated in several modules. However I did confirm the issue in this case is isolated to the single file node_modules/create-create-class/factory.js.

The circular reference in particular is the component value in this example. By changing the line from a regular (enumerable) property to one defined with Object.defineProperty (non-enumerable), the issue goes away. So for e.g.


function bindAutoBindMethod(component, method) {
  var boundMethod = method.bind(component);

  Object.define_property(boundMethod, '__reactBoundContext', { value: component });
  boundMethod.__reactBoundMethod = method;
  boundMethod.__reactBoundArguments = null;

That is because the component value contains the circular reference, because all components themselves have a reference to this __reactBoundContext value. So this becomes a problem when traversing the object tree based on its keys as the __reactBoundContext key is enumerated.

You can see my attempt at avoiding circular references here. However unfortunately it still isn't addressing my issue with this particular property. Perhaps my "fix" is naive or otherwise missing a case that I have yet to reproduce in isolation?

Thank you so much for all your hard work and attention to this particularly gritty issue! I must apologize if anything I've said doesn't make clear sense, I am just beginning to get familiar with JS development. ❤️

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions