React: Cannot call function start with '_' by using this.function_name in componentDidMount

Created on 6 Dec 2016  路  4Comments  路  Source: facebook/react

Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
Cannot call function start with '_' by using this.function_name in componentDidMount

  componentDidMount() {
        var windowWidth = $(window).width();
        $(window).resize(function () {
            if ($(window).width() != windowWidth) {
                windowWidth = $(window).width();
                this._resize_update();//can not find the function
                this.resize_update();//can find the function
            }
        }.bind(this));
    }
    _resize_update() {}
   resize_update() {}

What is the expected behavior?

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

Most helpful comment

As you can see here, it works with Babel:
https://jsfiddle.net/0jevL2xu/

So I鈥檓 closing this as not a React bug, but likely an issue with using a deprecated class transform that has special behavior.

All 4 comments

What are you using for transpiling ES6?

If it's not Babel, then I recommend switching to Babel. You might be relying on a very old unsupported version of class transform which has munging behavior for method names. If that's what you're using, I think you can fix it by adding @preventMunge to the file like here but in any case there are more issues with it and I don't recommend relying on it at all.

As you can see here, it works with Babel:
https://jsfiddle.net/0jevL2xu/

So I鈥檓 closing this as not a React bug, but likely an issue with using a deprecated class transform that has special behavior.

Thank you for replying. The solution above works for me.

Was this page helpful?
0 / 5 - 0 ratings