简体   繁体   English

在React项目中使用Ag-Grid时出错

[英]Error using ag-grid in a React project

I am trying to use ag-grid which looks promising along in my React project. 我正在尝试使用在我的React项目中看起来很有前途的ag-grid。

I made a very simple component with the most basic grid possible and I get an error: 我用最基本的网格制作了一个非常简单的组件,但出现错误:

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. 未捕获的错误:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到了:对象。 Check the render method of UserList 检查UserList的渲染方法

If I remove the <AgGridReact columnDefs={this.state.columnDefs} rowData={this.state.users} /> in the render() method, the page displays correctly again. 如果我在render()方法中删除<AgGridReact columnDefs={this.state.columnDefs} rowData={this.state.users} /> ,则页面将再次正确显示。

What could be wrong..? 有什么问题吗?

Here is my component code: 这是我的组件代码:

import React from 'react';
import ReactDOM from 'react-dom';

import AgGridReact from 'ag-grid-react';

$ = window.$ = window.jQuery = require('jquery');

var UserList = React.createClass({
    getInitialState: function () {
        return {
            showGrid: true,
            users: [],
            columnDefs: []
        };
    },

    getDefaultProps: function () {
        return {
        };
    },

    componentDidMount: function () {
        var self = this;
        // $.ajax({
        //     url: 'http://localhost:55010/api/v1/User',
        //     type: 'GET',
        //     data: null,
        //     contentType: "application/json; charset=utf-8",
        //     dataType: "json",
        //     success: function (data) {
        //         self.setState({ users: data });
        //     },
        //     error: function (jqXHR, b, c) {
        //     }
        // });
    },

    componentWillUnmount: function () {
    },

    // onShowGrid: function(show) {
    //     this.setState({
    //         showGrid: show
    //     });
    // },

    // onGridReady: function(params) {
    //     this.api = params.api;
    //     this.columnApi = params.columnApi;
    // },

    render: function () {
        return (
            <div>
                <h1>UserList</h1>

                <ul>
                    {this.state.users.map(function(u) {
                        return (
                        <li key={'u'+u.Id}>
                            {u.Id}, {u.Username}, {u.Email}  
                        </li>
                        );
                    })}
                </ul>

                <div style={{width: '800px'}}>
                    <div style={{padding: '4px'}}>
                        <div style={{height: 400}} className="ag-fresh">
                            <AgGridReact columnDefs={this.state.columnDefs} rowData={this.state.users} />
                        </div>
                    </div>
                </div>
            </div>
        );
    }
});

module.exports = UserList;

Well this is not a React specific thing, it is rather a ES6 thing :p. 嗯,这不是React特定的东西,而是ES6的东西:p。

So there are two ways to export in ES6. 因此,在ES6中有两种导出方法。 The first one is a default export which is written like this: 第一个是默认导出 ,其编写方式如下:

export default someVariable

If you export a variable this way, the you can import it in any other file this way: 如果以这种方式导出变量,则可以通过以下方式将其导入任何其他文件中:

import someVariable from '<path>'

The catch here is that you can even import your variable with a different name. 这里的要点是,您甚至可以使用其他名称导入变量。 Hence, 因此,

import someVariable2 from '<path>'

will also give me someVariable. 也会给我一些变量。 But there is a restriction as well. 但是也有一个限制。 You can have only one export default in a single file 单个文件中只能有一个导出默认值

Now the other way of exporting is the Named Export like this: 现在,导出的另一种方法是命名导出,如下所示:

export somevariable

In this case, you need to import it this way: 在这种情况下,您需要通过以下方式导入它:

import {someVariable} from '<path>'

In this case, the variable name ca't be changed. 在这种情况下,变量名称不能更改。 You need to import it with the same name which you mentioned while exporting. 您需要使用导出时提到的相同名称导入它。

So the AgGridReact component would be exported using the Named export. 因此,将使用Named导出来导出AgGridReact组件。 That is why you need to put {} around it. 因此,您需要在其中加上{}。

Wow I found it... 哇,我找到了...

I need to change 我需要改变

import AgGridReact from 'ag-grid-react';

to

import {AgGridReact} from 'ag-grid-react';

Any React expert can explain the difference? 任何React专家都可以解释差异吗? O_o O_o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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