Monday, 10 April 2017

React.js function not firing

I recently saw a warning pop up on one of my projects saying that the React.createClass is now deprecated so I'm going through some of the code to make it compatible with the new recommendations.

One thing I've run into though is that one of my functions doesn't seem to fire like it used to.

class Example extends React.Component {

    constructor() {

        super();
        this.state = { content: "Initialize" }

    }

    changeScreen(newScreen) {

        // this fires
        alert("fired 01");

        // this function does not
        this.testFunc;

        var screen = "";

        switch(newScreen) {
            case "one":
                screen = "var01";
                break;
            case "two":
                screen = "var02";
                break;
            default:
                screen = "failed";
                break;
        }

    }

    testFunc() {
        alert("fired 02");
    }

    render() {

        return (
            <div>
                <External.Element execChangeScreen={this.changeScreen} />
                {this.state.content}
            </div>
        );

    }

}

ReactDOM.render (
    <Example />,
    document.getElementById("app")
);

I can't seem to get testFunc to fire, I've tried calling like follows

this.testFunc();
this.testFunc;
() => this.testFunc();

I'm not sure why but I think it may have something to do with this



via Trent

No comments:

Post a Comment