将工作和功能正常的reactjs组件转换为基于类的组件

时间:2018-07-23 23:43:07

标签: reactjs visual-studio uncaught-typeerror

我试图弄清楚如何在Visual Studio 2017环境中使用ASP.Net MVC框架转换此功能性React组件 下面的代码可以正常工作:

    var ProductGridRow = React.createClass({
        render : function(){
            return (
                <tr>
                    <td>{this.props.item.ProductName}</td>
                    <td>{this.props.item.Price}</td>

                </tr>
            );
        }
    });

    var ProductGridTable = React.createClass({
        getInitialState: function(){
            return {
                items:[]
            }
        },
        componentDidMount:function(){
            // Fetch data via ajax *@
            $.get(this.props.dataUrl, function(data){
                if(this.isMounted()){
                    this.setState({
                        items: data
                    });
                }
            }.bind(this));
        },
        render : function(){
            var rows = [];
            this.state.items.forEach(function(item){
                rows.push(
                    <ProductGridRow key={item.Id} item={item} />);
            });
            return (
                <table className="table table-bordered table-responsive">
                    <thead>
                    <tr>
                        <th>Product Name</th>
                        <th>Price</th>

                    </tr>
                    </thead>
                    <tbody>
                    {rows}
                    </tbody>
                </table>);
        }
    });
    ReactDOM.render(
        <ProductGridTable dataUrl="/home/getProductData" />,
        document.getElementById('griddata')
    );

但是,当我尝试将上述代码转换为具有符合当前实践的基于类的组件时,以及当我尝试这样做时:

    class ProductGridRow extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                productRow: []
            }

            render()
            {
                return (
                    <tr>
                        <td>{this.props.item.ProductName}</td>
                        <td>{this.props.item.Price}</td>

                    </tr>
                );
            }
        }
    }

    class ProductGridTable extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                productList: []
            };
            this.loadData = this.loadData.bind(this);
        }

        componentDidMount(){
                       // Fetch data via ajax 
            this.loadData();
        }

        loadData() {
            $.get(this.props.dataUrl,
                function(data) {
                    if (this.isMounted()) {
                        this.setState({
                            items: data
                        });
                    }
                });
        }


            render(){
                var rows = [];
                this.state.productList.forEach(function(item) {
                    rows.push(
                        <ProductGridRow key={item.Id} item={item}/>);
                })
                return (
                    <table className="table table-bordered table-responsive">
                        <thead>
                        <tr>
                            <th>Product Name</th>
                            <th>Price</th>

                        </tr>
                        </thead>
                        <tbody>
                        {rows}
                        </tbody>
                    </table>);
            }
        }



        ReactDOM.render(<ProductGridTable dataUrl="/home/getProductData" /> 
        ,document.getElementById('griddata'));

,但最终出现“未捕获的TypeError:this.isMounted不是函数”。转换后不确定什么是正确的代码。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

isMounted已过时。只需在AJAX成功处理程序回调中删除对if (this.isMounted()) {...}的调用周围的setState块即可。

https://reactjs.org/blog/2015/12/16/ismounted-antipattern.html