【问题标题】:Why is my React component unable to pick up the library I am requiring?为什么我的 React 组件无法获取我需要的库?
【发布时间】:2016-01-14 16:36:53
【问题描述】:

我正在尝试在我的 React 项目中使用 FullCalendar.io。

我已经使用npm install fullcalendar 安装了它。

我在我的目录中看到node_modules/fullcalendar

在我的项目中,我正在使用:

var Scheduler = require('fullcalendar');

然后我有:

var Calendar = React.createClass({

    componentDidMount: function () {

        $('#calendar').fullCalendar();
    },

    render: function () {
        return (
            <div id='calendar'>
            </div>
        );
    }
});

但我收到此错误:Uncaught TypeError: $(...).fullCalendar is not a function

(旁注 - 需要其他模块可以正常工作。我可以执行 var moment = require('moment') 然后 console.log(moment()) 并输出一个新的矩变量。)

【问题讨论】:

  • 你有 jquery $ 作为模块吗?
  • 很奇怪。我还没有需要 jquery,以前的 jquery 函数工作得很好——我只是测试了它(它在我的索引文件中)。然而,添加 var $ = require('jquery') 允许 fullCalendar 启动。谢谢。

标签: javascript node.js reactjs fullcalendar


【解决方案1】:

确保在每个组件中都需要所有需要启动的库。仅将它放在需要转换/捆绑的文件的索引文件中是行不通的。 (在 React 应用程序中需要使用 jQuery 有点可悲,因为 jQuery 是为了克服不知道元素在哪里/是什么的问题,而在 React 中你总是知道你的元素是什么)

【讨论】:

  • 我以后一定会这样做的。不过我仍然很困惑,因为我能够使用 jQuery 隐藏组件(不是我做过,但我能够)而不需要先使用模块。无论如何,是的,使用 jQuery 有点难过,但我需要它用于 FullCalendar!
  • 一旦站点被加载,任何无范围的 jquery 都可以正常运行,但它也会与 React 的重新呈现方式发生冲突(如果 React 组件的 props 或 state 更新,任何 jquery 更改都会被擦除)。至于FullCalendar需要jQuery,肯定有非jquery日历解决方案吗? github.com/react-component/calendar 例如?
  • 那里的日历非常好,但我们使用的是 FullCalendar 的日程安排方面 - 带有事件的拖放选项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 2014-10-25
  • 2016-06-25
相关资源
最近更新 更多