【问题标题】:Importing Editor from draft-js-plugins-editor causes TypeErrors on empty project从 Draft-js-plugins-editor 导入编辑器会导致空项目出现 TypeErrors
【发布时间】:2017-10-14 03:24:59
【问题描述】:

我在 Webstorm 中创建了一个新的 React 项目。我已经安装了draft-jsdraft-js-plugins-editor,以及插件draft-js-hashtag-plugindraft-js-mathjax-plugin(使用节点)。

我一直在关注他们的“入门”on their Github,但是该示例对我不起作用。刚写完

import Editor from 'draft-js-plugins-editor';

我收到 TypeError: Cannot read property 'object' of undefined 错误。

./node_modules/draft-js-plugins-editor/lib/Editor/index.js
node_modules/draft-js-plugins-editor/lib/Editor/index.js:177
174 | }(_react.Component);
175 |
176 | PluginEditor.propTypes = {
    > 177 |   editorState: _react2.default.PropTypes.object.isRequired,
178 |   onChange: _react2.default.PropTypes.func.isRequired,
179 |   plugins: _react2.default.PropTypes.array,
180 |   defaultKeyBindings: _react2.default.PropTypes.bool,

我的最小示例代码:

import React, { Component } from 'react';
import Editor from 'draft-js-plugins-editor'; // Error upon doing this
import createHashtagPlugin from 'draft-js-hashtag-plugin';
import { EditorState } from 'draft-js';

const hashtagPlugin = createHashtagPlugin();

const plugins = [
    hashtagPlugin,
];

export default class MyEditor extends Component {

    state = {
        editorState: EditorState.createEmpty(),
    };

    onChange = (editorState) => {
        this.setState({
            editorState,
        });
    };

    render() {
        return (
            <Editor
                editorState={this.state.editorState}
                onChange={this.onChange}
                plugins={plugins}
            />
        );
    }
}

【问题讨论】:

    标签: javascript node.js reactjs draftjs draft-js-plugins


    【解决方案1】:

    出现此错误是因为您使用的是最新 (16th) 版本的 React.js。 In this 博文(React.js 第 16 版公告)你可以阅读:

    15.x 中引入的弃用已从核心中删除 包裹。 React.createClass 现在可以作为 create-react-class 使用, React.PropTypes 作为 prop-types...

    如果你在 React 16 中使用它们,所有仍然访问 React.PropTypes 的旧包都将被破坏。

    如果你想使用draft-js-plugins-editor,你必须将你的 react 版本降级到版本 15。例如,npm 使用这个命令

    npm install react@15.4.2 --save

    或者这个yarn

    yarn add react@15.4.2

    【讨论】:

      猜你喜欢
      • 2011-06-20
      • 2022-08-24
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      • 2014-09-18
      相关资源
      最近更新 更多