简体   繁体   中英

Refreshing App State on Child Component Change

I have a list of elements. When I click an up or down icon, I would like the list to rearrange itself and, finally, for the app to rerender itself so I can see the change reflected in the DOM.

Changing list position works. I'm running into issues when I try to run the refreshState method. I'm passing the function as a property of the child but calling that property returns undefined function.

Q: How do I call a component's method from its child component?

Code:

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <script src="http://fb.me/react-with-addons-0.12.2.js"></script>
    <script src="http://fb.me/JSXTransformer-0.12.2.js"></script>
</head>
<body>
    <style>
        /* CSS */
        span {
            margin:0 0 0 10px;
        }
    </style>
    <div id="app"></div>
    <script type="text/jsx">
        // React

        var _data = ['Red', 'Blue', 'Green'];


        function getState() {
            return {
                colors: _data,
            }
        };


        Array.prototype.swap = function(a, b) {
            var temp = this[a];
            this[a] = this[b];
            this[b] = temp;
        };


        var App = React.createClass({

            getInitialState: function() {
                return getState();
            },

            render: function() {
                var colors = this.state.colors.map(function(color) {
                    return (
                        <Color name={color} refreshState={this.refreshState} />
                    )
                });
                return (
                    <ul>{colors}</ul>
                )
            },

            refreshState: function() {
                return this.setState(getState());
            },

        });


        var Color = React.createClass({

            moveUp: function() {
                var current = _data.indexOf(this.props.name),
                    above = current - 1;

                if (above >= 0) {
                    _data.swap(current, above);
                }

                return this.props.refreshState();
            },

            moveDown: function() {
                var current = _data.indexOf(this.props.name),
                    below = current + 1;

                if (below < _data.length) {
                    _data.swap(current, below);
                }

                return this.props.refreshState();
            },

            render: function() {
                return (
                    <li>
                        <strong>{this.props.name}</strong>
                        <span onClick={this.moveUp}>^</span> 
                        <span onClick={this.moveDown}>v</span>
                    </li>
                )
            },

        });


        React.render(<App />, document.getElementById('app'));
    </script>
</body>
</html>

Noticed you have solved the question, but thought I'd mention that you can pass a scope to .map which means no need to cache scope for the purpose you describe:

this.state.colors.map(function(color) {
    return (
        <Color 
            key={_data.indexOf(color)}
            name={color}
            refresh={this.refreshState}
        />
    )
}, this); // pass the scope to .map

I was trying to call this.refreshState within the map method. This, of course, does not have the same scope as the render method. The solution was to store the scope in a variable:

var refresh = this.refreshState;

Then use that variable within the map method:

... refreshState={refresh} ...

Always be aware of your scope!

If you have multiple functions that aren't within the local scope then you can store this in a variable.

var self = this;
z.map(function(arg) {
    x={self.refreshState} y={self.otherThing}

And for the curious, the finished result:

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <script src="http://fb.me/react-with-addons-0.12.2.js"></script>
    <script src="http://fb.me/JSXTransformer-0.12.2.js"></script>
</head>
<body>
    <style>
        /* CSS */
        span {
            margin:0 0 0 10px;
        }
    </style>
    <div id="app"></div>
    <script type="text/jsx">
        // React

        var _data = ['Red', 'Blue', 'Green'];


        function getState() {
            return {
                colors: _data,
            }
        };


        Array.prototype.swap = function(a, b) {
            var temp = this[a];
            this[a] = this[b];
            this[b] = temp;
        };


        var App = React.createClass({

            getInitialState: function() {
                return getState();
            },

            refreshState: function() {
                return this.setState(getState());
            },

            render: function() {
                var self = this;
                var colors = this.state.colors.map(function(color) {
                    return (
                        <Color 
                            key={_data.indexOf(color)}
                            name={color}
                            refresh={self.refreshState}
                        />
                    )
                });
                return (
                    <ul>{colors}</ul>
                )
            },

        });


        var Color = React.createClass({

            propTypes: {
                name: React.PropTypes.string,
                refresh: React.PropTypes.func.isRequired,
            },

            moveUp: function() {
                var current = _data.indexOf(this.props.name),
                    above = current - 1;

                if (above >= 0) {
                    _data.swap(current, above);
                }

                return this.props.refresh();
            },

            moveDown: function() {
                var current = _data.indexOf(this.props.name),
                    below = current + 1;

                if (below < _data.length) {
                    _data.swap(current, below);
                }

                return this.props.refresh();
            },

            render: function() {
                return (
                    <li>
                        <strong>{this.props.name}</strong>
                        <span onClick={this.moveUp}>^</span> 
                        <span onClick={this.moveDown}>v</span>
                    </li>
                )
            },

        });


        React.render(<App />, document.getElementById('app'));
    </script>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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